When including a file, in PHP, you are not including anything into the current file, but into the current execution
Once a file has been included, it's content is known by the PHP interpreter -- and will remain known until the end of the execution of the current script.
It's a bit like copy-pasting : with your current index.php
file, it's like if you had taken the code from foo.php
and bar.php
and copy-pasted into index.php
.
So, yes, once foo.php
has been loaded once, you can access what's defined by it from bar.php
-- even if those two are included by index.php
.
(What you means by access is a bit vague, though : you'll have to declare some methods as static, or instanciate the foo
class, ... But all that will be possible from inside the body on a method declared in bar
)
As a sidenote : you should be careful with your file inclusions : if you include twice a file that declares a class, for example, you'll get a fatal error, because it's forbidden to define two classes with the same name.
Remember the copy-pasting idea I wrote about earlier ? Includind a file twice, even from two distinct files, is like copy-pasting the same code twice...
To avoid that problem, we often use include_once
which, as its name indicates, makes sure that a given file can only be included only once.