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

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

am 15.10.2007 22:41:02 von duzhidian

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.

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

am 15.10.2007 22:56:17 von Jerry Stuckle

duzhidian@gmail.com wrote:
> 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.
>
>

Don't use relative paths. Use absolute paths. If you're running this
under a webserver, you can get to the web server's root with
$_SERVER['DOCUMENT_ROOT'] and go from there.


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

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

am 15.10.2007 23:16:15 von duzhidian

If I move the programs to other places, using absolute paths will
suffer, need to change every file.



On Oct 15, 4:56 pm, Jerry Stuckle wrote:
> duzhid...@gmail.com wrote:
> > 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.
>
> Don't use relative paths. Use absolute paths. If you're running this
> under a webserver, you can get to the web server's root with
> $_SERVER['DOCUMENT_ROOT'] and go from there.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

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

am 16.10.2007 00:05:35 von Lars Eighner

In our last episode, <1192482975.727615.56570@i13g2000prf.googlegroups.com>,
the lovely and talented duzhidian@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
Countdown: 462 days to go.
What do you do when you're debranded?

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

am 16.10.2007 01:25:15 von Michael Fesser

..oO(duzhidian@gmail.com)

>If I move the programs to other places, using absolute paths will
>suffer, need to change every file.

As long as you keep the folder structure the same, you won't have any
problem. You can always use $_SERVER['DOCUMENT_ROOT'] as your starting
point to build your absolute paths. It becomes even more convenient if
you define some path constants in one of your main PHP files that's
included in every page, and use these constants when you include the
other files. If you change the server, but keep the folder structure,
you would only have to adjust a single file at most, if any.

Absolute paths work always, and you don't have think much about them.
Define them once in a single place - and then just use them.

Micha

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

am 16.10.2007 01:27:54 von Jerry Stuckle

duzhidian@gmail.com wrote:
>
> On Oct 15, 4:56 pm, Jerry Stuckle wrote:
>> duzhid...@gmail.com wrote:
>>> 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.
>> Don't use relative paths. Use absolute paths. If you're running this
>> under a webserver, you can get to the web server's root with
>> $_SERVER['DOCUMENT_ROOT'] and go from there.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
>
>

> If I move the programs to other places, using absolute paths will
> suffer, need to change every file.
>
>
(top posting fixed)

Not if you use the method I mentioned.

And please don't top post.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 16.10.2007 01:28:40 von Jerry Stuckle

Lars Eighner wrote:
> In our last episode, <1192482975.727615.56570@i13g2000prf.googlegroups.com>,
> the lovely and talented duzhidian@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.
jstucklex@attglobal.net
==================

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

am 16.10.2007 02:44:07 von luiheidsgoeroe

On Tue, 16 Oct 2007 01:27:54 +0200, Jerry Stuckle
wrote:

> duzhidian@gmail.com wrote:
>> On Oct 15, 4:56 pm, Jerry Stuckle wrote:
>>> duzhid...@gmail.com wrote:
>>>> failed to open stream: No such file or directory



>>> Don't use relative paths. Use absolute paths. If you're running this
>>> under a webserver, you can get to the web server's root with
>>> $_SERVER['DOCUMENT_ROOT'] and go from there.
>
> > If I move the programs to other places, using absolute paths will
> > suffer, need to change every file.
> >
> >
> (top posting fixed)
>
> Not if you use the method I mentioned.

It would cause trouble if he decides to put them somewhere else in the
tree though....

I usually use this kind of reasoning:

1. A custom/the main script uses absolute paths from DOCUMENT_ROOT
2. Little reusable add-in modules (which could be anywhere) use relative
paths, but check & store getcwd(), change it to their path, do the file
action and immediately change it back to the old value.

--
Rik Wasmus

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

am 16.10.2007 02:49:11 von Jerry Stuckle

Rik Wasmus wrote:
> On Tue, 16 Oct 2007 01:27:54 +0200, Jerry Stuckle
> wrote:
>
>> duzhidian@gmail.com wrote:
>>> On Oct 15, 4:56 pm, Jerry Stuckle wrote:
>>>> duzhid...@gmail.com wrote:
>>>>> failed to open stream: No such file or directory
>
>
>
>>>> Don't use relative paths. Use absolute paths. If you're running this
>>>> under a webserver, you can get to the web server's root with
>>>> $_SERVER['DOCUMENT_ROOT'] and go from there.
>>
>> > If I move the programs to other places, using absolute paths will
>> > suffer, need to change every file.
>> >
>> >
>> (top posting fixed)
>>
>> Not if you use the method I mentioned.
>
> It would cause trouble if he decides to put them somewhere else in the
> tree though....
>
> I usually use this kind of reasoning:
>
> 1. A custom/the main script uses absolute paths from DOCUMENT_ROOT
> 2. Little reusable add-in modules (which could be anywhere) use relative
> paths, but check & store getcwd(), change it to their path, do the file
> action and immediately change it back to the old value.
>
> --Rik Wasmus
>

That's true. But I always use the same format. Included files go below
the website's root directory. Others go in a specific directory in the
root. But there are very few of the latter :-)

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

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

am 16.10.2007 03:49:05 von duzhidian

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.

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

am 16.10.2007 03:53:53 von Jerry Stuckle

duzhidian@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.
jstucklex@attglobal.net
==================

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

am 16.10.2007 08:10:33 von Tim Roberts

Jerry Stuckle wrote:

>duzhidian@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, timr@probo.com
Providenza & Boekelheide, Inc.

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

am 16.10.2007 09:32:27 von luiheidsgoeroe

On Tue, 16 Oct 2007 03:49:05 +0200, duzhidian@gmail.com =

wrote:
> 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 =3D 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

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

am 16.10.2007 09:44:43 von duzhidian

On Oct 16, 2:10 am, Tim Roberts wrote:
> Jerry Stuckle wrote:
> >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.

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

am 16.10.2007 15:04:47 von Jerry Stuckle

Tim Roberts wrote:
> Jerry Stuckle wrote:
>
>> duzhidian@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.
jstucklex@attglobal.net
==================

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

am 16.10.2007 20:55:14 von Alexey Kulentsov

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.

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

am 17.10.2007 14:08:45 von AnrDaemon

Greetings, Alexey Kulentsov.
In reply to Your message dated Tuesday, October 16, 2007, 22:55:14,

AK> You have to use include_once
AK> dirname(__FILE__).'some_path_to_file.php' to get portable code
AK> independent from server.

dirname(__FILE__).'/some_path_to_file.php'

Exactly.


--
Sincerely Yours, AnrDaemon

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

am 17.10.2007 14:27:18 von luiheidsgoeroe

On Tue, 16 Oct 2007 20:55:14 +0200, Alexey Kulentsov wrote:

> 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

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

am 21.10.2007 06:11:50 von Tim Roberts

Jerry Stuckle wrote:

>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. ), THEN the current
directory is skipped and it goes straight to the -I path.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

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

am 21.10.2007 15:48:04 von unknown

Post removed (X-No-Archive: yes)

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

am 21.10.2007 16:06:10 von Jerry Stuckle

Tim Roberts wrote:
> Jerry Stuckle wrote:
>
>> 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.
>

You're wrong. Compilation is done by the compiler, under the rules
defined for that program. Execution is done by the program, under rules
defined for that program.

In the case of the C #include statement, how it works is defined by the
preprocessor, using the -I compiler command line option. That option
defines the order in which libraries which are included at compilation
(actually pre-processing) time. And once the program is compiled, the
included files are no longer needed.

In PHP, include is an executable statement, just like echo is. The
statement is executed at run time, using options defined in the php.ini
file. Additionally, the included file must exist when the program is
executed.

As I said. The only thing they have in common is the word "include".
Their operation is quite different.

>> 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. ), THEN the current
> directory is skipped and it goes straight to the -I path.

That's because that is the way the preprocessor works.

But to say the rules are different is wrong. The rules of execution are
exactly the same for C and PHP programs. C programs never execute an
include statement - because the preprocessor has handled it.

However, C programs can deal with files, etc. And the operation when
they are executed is exactly the same as when a php script is executed.
C programs will look in the current directory for the file. PHP
programs will look in the current directory for the file.

In this way, PHP's include statement is much more closely related to C's
(and PHP's) fopen() call. Both are executed statements, and operate
similarly.

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