Can you include PHP files at any place in your code or do you have to do it at the top of the file?
Printable View
Can you include PHP files at any place in your code or do you have to do it at the top of the file?
At any place at all. Here's something I do:
Code:function loadComponent($name)
{
$class = $this->config[$name]['class'];
include_once("$class.php");
return new $class;
}
thanks
There's a gotcha here, though. Suppose:
a.php
b.php:Code:$global = 'hello, there';
include('b.php');
function foo()
{
include ('b.php');
}
The first inclusion prints "hello, there". The second prints nothing. That's because PHP files are included as if the include statement was directly replaced by the contained code.Code:echo $global;
so you'd need a
inside the function before the include('b.php').PHP Code:global $global