Originally posted by mrhappiness
if you include a file all code within that file is being executed
Actually no, when you include a file the code gets parsed, not executed. Any code outside of a function or class declaration would get executed.
In answer - yes including files will have noticeable effect on the speed of your program. 40 includes means 40 file reads and maybe alot of parsing. It could mount up. PHP 5 offers the __autoload() special function which loads (class) files in only when you try to instatiate the class. This can be imitated in PHP 4 but it's not nearly so good, and requires that you call a wrapper class directly.
class ClassCaller
{
function &call($classname, $args = array())
{
if(class_exists($classname))
{
return new $classname(implode(',', $args)); // Assumes $args is array
}
elseif(file_exists($file = $classdir . "/" . $classname. ".php"))
{
require_once($file);
return new $classname($args);
}
else
{
die("Call to undefined class $classname()");
}
}
}
class MyTestClass
{
function myTestClass($string)
{
echo $string;
}
}
$obj =& ClassCaller::call('MyTestClass', array('test')); // This replaces the usual 'new ObjName($args);'
Also this assumes that you want to create a new object, and that each class lives in it's own file, and the filename is the same as the classname (a limitation of __autoload() in any case).