1

lets say I have four PHP files as such:

www/global.php

<?php 
function doAwesomeStuff() {}

www/child1.php

<?php
include ("global.php");
function Something1() {}

www/child2.php

<?php
include ("global.php");
function Something2() {}

www/subdirectory/grandchild.php

<?php
include ("../global.php");
include ("../child2.php");
function Something3() {}

I run into the problem of including global twice in one case, but if we keep getting complicated, the include's are calling directory paths relative to the called file, not the included file, which is a real pain logically.

Any solutions to this?

7 Answers 7

5

Personally, I'd say keep better track of dependencies or have a central includes files, but failing that, there's always include_once().

Note: include_once() and require_once() are both much slower than plain-old include() and require().

Sign up to request clarification or add additional context in comments.

6 Comments

Just out of curiosity, do you have anything to backup that they are slower? I would be interested to read up on that!
@Brad F Jacobs: even more interesting this assumption turns out to be wrong. blog.seeit.org/2010/06/…
arin.me/blog/… and notmysock.org/blog/php/include_once-mostly-harmless.html ... not saying either is necessarily a great article, but they both include some profiling.
@mario: The problem with that test is that it is including in one file. OF COURSE include_once and require_once are going to end up looking faster -- they're NOT going to be including the same file over-and-over (not to mention firing up the PHP parser), while include and require are. Instead of 10,000 times in one file it should've been one file run 10,000 times. That is a very poorly designed test and contradicts every other benchmark done on the subject. The results were also severely contested in the comments of the article you just linked.
@Dereleased: Actually they've created 10,000 stub files. So it does have some relevancy. But it's a synthetic test nevertheless. So i don't think it has much impact on real world scripts. And with opcode caches the differences become negligible as include starts to behave like include_once anyway. However as that post also highlighted include was indeed faster than _once for PHP4 and maybe early PHP5 versions. Also the APC include_once problems are mostly gone, so a custom reimplementation/workaround is no longer necessary.
|
3

In grandchild.php, do this:

set_include_path("..");
require_once("global.php");
require_once("child2.php");

And in all the other files, it's better to replace include with require_once, too, to avoid going on after not being able to include a file ("require") and to avoid including a file multiple times ("once").

2 Comments

I forgot about the set_include_path('..'); Excellent answer, +1
Set include path is the answer. Using this to point to your project's root, you can then require_once all of your files accordingly.
1

Look at require_once, this way a file it's included only if it has not been included before.

Comments

0

You can use require_once(). Ofcourse it's better to make a solid dependecy of the files. But require_once will check "if the file has already been included, and if so, not include (require) it again".

Comments

0

I think you are looking for require_once

Comments

0

As already mentioned require_once 'includes.php' will do the job RE including too much.

I have played around with using getcwd() to return the path to help with what you need to include but I have nothing concrete to show you unfortunately.

Comments

0

In addition to the require_once part, you can probably neaten up your code a bit using php's __autoload magic method in a central config file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.