472,982 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

The principle of include files in PHP, not as in C++

failed to open stream: No such file or directory

I have the programs like this structure:

a.php is locate at .
b.php is located at ./lib
c.php is located at ./lib/sublib

a.php uses a function at b.php while b.php using a function at c.php

In a.php, it says: include_once(./lib/b.php) as it uses one function
in b.php
a.php is not supposed to know that b.php is using c.php, so it should
not include c.php.
In b.php, it says: include_once(./sublib/c.php) since it uses a
function in c.php.
b.php is not supposed to know that a.php uses itself, so it should not
include lib as include_once(../lib/sublib/c.php), but it does not
work.
The problem is: "... failed to open stream: No such file or directory
of c.php. .."

If using include_once(../lib/sublib/c.php) in b.php, the compiler is
happy. But it violates the general principle of who include files
only considering itself at it own position, not considering others who
use it (also, it is not supposed to know it) as in C++.
Any hints how to obey the above principle?

Thanks.

D.

Oct 15 '07 #1
12 1710
In our last episode, <11*********************@i13g2000prf.googlegroups. com>,
the lovely and talented du*******@gmail.com broadcast on comp.lang.php:
If I move the programs to other places, using absolute paths will
suffer, need to change every file.
Generally I use a project.ini where I use set_ini to modify the include path
for the project, then I never have to use paths in includes except for the
path to project.ini (possibly), but since it is a perl one-line to change
that (if necessary) I don't see the problem.

PS: why ./lib/b.php instead of lib/b.php ?
--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 462 days to go.
What do you do when you're debranded?
Oct 15 '07 #2
Lars Eighner wrote:
In our last episode, <11*********************@i13g2000prf.googlegroups. com>,
the lovely and talented du*******@gmail.com broadcast on comp.lang.php:
>If I move the programs to other places, using absolute paths will
suffer, need to change every file.

Generally I use a project.ini where I use set_ini to modify the include path
for the project, then I never have to use paths in includes except for the
path to project.ini (possibly), but since it is a perl one-line to change
that (if necessary) I don't see the problem.

PS: why ./lib/b.php instead of lib/b.php ?

Until you run into two files with the same name... As in two different
packages.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 15 '07 #3

Thanks for your guys info. All your suggestions is to use absolute
path somewhere.

In fact, here is my scenario, *SAME* file name, different location:

a. one php is locate at . called ./mylib.php
b. another php file is located at ./lib/mylib.php
c. third php file is located at ./lib/sublib/mylib.php
a calls a function in b while b calls a function in c.

What I need is just a clear picture how to include files in a and b,
i.e. in ./mylib.php and in ./lib/mylib.php without any trouble when
moving and future extension.
I can organize the codes, but it does not seems as transparent as what
c/c++ does as one file at root have to consider it's grandsons'
functions (location), not supposed to do that in c/c++.

Thanks again.

Du.

Oct 16 '07 #4
du*******@gmail.com wrote:
Thanks for your guys info. All your suggestions is to use absolute
path somewhere.

In fact, here is my scenario, *SAME* file name, different location:

a. one php is locate at . called ./mylib.php
b. another php file is located at ./lib/mylib.php
c. third php file is located at ./lib/sublib/mylib.php
a calls a function in b while b calls a function in c.

What I need is just a clear picture how to include files in a and b,
i.e. in ./mylib.php and in ./lib/mylib.php without any trouble when
moving and future extension.
I can organize the codes, but it does not seems as transparent as what
c/c++ does as one file at root have to consider it's grandsons'
functions (location), not supposed to do that in c/c++.

Thanks again.

Du.

You're still using relative paths. Anything starting with ".", ".." or
a path name is relative.

Absolute paths start with / in linux/unix.

It works exactly the same in C/C++. Relative paths there are relative
to the executable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 16 '07 #5
Jerry Stuckle <js*******@attglobal.netwrote:
>du*******@gmail.com wrote:
>>
I can organize the codes, but it does not seems as transparent as what
c/c++ does as one file at root have to consider it's grandsons'
functions (location), not supposed to do that in c/c++.

You're still using relative paths. Anything starting with ".", ".." or
a path name is relative.

Absolute paths start with / in linux/unix.

It works exactly the same in C/C++. Relative paths there are relative
to the executable.
You missed the point. He's talking about #include files, and he is quite
correct. In C/C++, you never ever ever use an absolute pathname in an
#include statement. Further, the preprocessor rule is that relative paths
in an #include statement are relative to the directory that contains the
file being scanned.

PHP's rules are different. Not better, not worse. Just different.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 16 '07 #6
On Tue, 16 Oct 2007 03:49:05 +0200, du*******@gmail.com
<du*******@gmail.comwrote:
Thanks for your guys info. All your suggestions is to use absolute
path somewhere.
I gave you a perfectly serviceable solution.
1. Store current working dir somewhere ( $cwd = getcwd(); ).
2. Set current working dir to the one of the file
( chdir(dirname(__FILE__)); ).
3. Do a relative include/require/fopen/etc..
4. Restore the current working dir to the one you stored ( chdir($cwd); ).
--
Rik Wasmus
Oct 16 '07 #7
On Oct 16, 2:10 am, Tim Roberts <t...@probo.comwrote:
Jerry Stuckle <jstuck...@attglobal.netwrote:
duzhid...@gmail.com wrote:
I can organize the codes, but it does not seems as transparent as what
c/c++ does as one file at root have to consider it's grandsons'
functions (location), not supposed to do that in c/c++.
You're still using relative paths. Anything starting with ".", ".." or
a path name is relative.
Absolute paths start with / in linux/unix.
It works exactly the same in C/C++. Relative paths there are relative
to the executable.

You missed the point. He's talking about #include files, and he is quite
correct. In C/C++, you never ever ever use an absolute pathname in an
#include statement. Further, the preprocessor rule is that relative paths
in an #include statement are relative to the directory that contains the
file being scanned.

PHP's rules are different. Not better, not worse. Just different.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
Yes, you are right. I was trying using C/C++ rules onto PHP programs
and it caused some problems. I can figure it out. Anyway, thanks
your guys.

Oct 16 '07 #8
Tim Roberts wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>du*******@gmail.com wrote:
>>I can organize the codes, but it does not seems as transparent as what
c/c++ does as one file at root have to consider it's grandsons'
functions (location), not supposed to do that in c/c++.
You're still using relative paths. Anything starting with ".", ".." or
a path name is relative.

Absolute paths start with / in linux/unix.

It works exactly the same in C/C++. Relative paths there are relative
to the executable.

You missed the point. He's talking about #include files, and he is quite
correct. In C/C++, you never ever ever use an absolute pathname in an
#include statement. Further, the preprocessor rule is that relative paths
in an #include statement are relative to the directory that contains the
file being scanned.

PHP's rules are different. Not better, not worse. Just different.
No, you missed the point.

Compilation is not the same as execution. When PHP is executed, it's
include statement works just alike any file operation in C/C++ when that
program is executed.

Just because they are both "include" statements does not mean they are
the same. In PHP they are executed. In C/C++ they are handled by the
preprocessor. Completely different environment with completely
different results.

And you are incorrect. In C/C++, include files are relative to the list
of directories in the -I (INCLUDE) compiler option (which can also be
specified in some IDE's). And you can use absolute paths in #include
statements.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 16 '07 #9
Tim Roberts wrote:
>
PHP's rules are different. Not better, not worse. Just different.
Yes, not worse but just fully idiotic.
You have to use include_once
dirname(__FILE__).'some_path_to_file.php' to get portable code
independent from server.
Oct 16 '07 #10
Greetings, Alexey Kulentsov.
In reply to Your message dated Tuesday, October 16, 2007, 22:55:14,

AK You have to use include_once
AKdirname(__FILE__).'some_path_to_file.php' to get portable code
AKindependent from server.

dirname(__FILE__).'/some_path_to_file.php'

Exactly.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Oct 17 '07 #11
On Tue, 16 Oct 2007 20:55:14 +0200, Alexey Kulentsov <ak**@inbox.ruwrote:
Tim Roberts wrote:
> PHP's rules are different. Not better, not worse. Just different.
Yes, not worse but just fully idiotic.
You have to use include_once
dirname(__FILE__).'some_path_to_file.php' to get portable code
independent from server.
PHP was never meant to be independant from a server.... It can be, not the
general idea though.
--
Rik Wasmus
Oct 17 '07 #12
Jerry Stuckle <js*******@attglobal.netwrote:
>Tim Roberts wrote:
>>
You missed the point. ...
PHP's rules are different. Not better, not worse. Just different.

No, you missed the point.

Compilation is not the same as execution. When PHP is executed, it's
include statement works just alike any file operation in C/C++ when that
program is executed.

Just because they are both "include" statements does not mean they are
the same. In PHP they are executed. In C/C++ they are handled by the
preprocessor. Completely different environment with completely
different results.
Nonsense. The distinction between compilation and interpolation is
entirely artificial. PHP can be compiled, just as C can be interpreted.
We're talking about source files including other source files. The concept
is the same. PHP has different rules from C. It's just that simple, and I
don't know why you want to make it more complicated.
>And you are incorrect. In C/C++, include files are relative to the list
of directories in the -I (INCLUDE) compiler option (which can also be
specified in some IDE's). And you can use absolute paths in #include
statements.
I never denied that. However, I am NOT incorrect. The first place C and
C++ look for an "included" file is in the directory of the file currently
being scanned. If not found there, only then does the preprocessor move on
to the -I list.

If the #include uses angle brackets (i.e. <stdio.h>), THEN the current
directory is skipped and it goes straight to the -I path.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 21 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chad Scharf | last post by:
I have a legacy ASP application running on IIS 6.0 (Windows Server 2003 Web Edition) that is throwing an error when processesing a certain asp page that has about 200 or so include directives. ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
7
by: mescaline | last post by:
Hi, Suppose a_file.cpp contains a function a_function() Now to include it in main_file.cpp I just do #include "a_file.cpp" and I'm all set. i recently came across this seemingly roundabout...
3
by: bluekite2000 | last post by:
I have //vector_header.h template <typename T> class Vector { .... } //vector_func.h #include vector_header.h
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
1
by: ya man | last post by:
when i use #include <iostream.h> in some files i get lots of error messages of the kind 'ambiguous symbol this is solved when i use #include <iostream why is that ? and can i use #include...
16
by: Chris Shearer Cooper | last post by:
In our Visual Studio 2005 application, we have several of our application's H files that are #included into stdafx.h. What is odd, is that when we change those application H files, VS2005...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.