__autload dilemma, not working

Hi all,

Here is my problem. In my file I have an __autoload function and it is called as long as classes are called from the file the __autoload function is placed in. But if I include a file in the file which __autoload is placed and try to call a class __autoload is not called.

Example:
file.php


function __autoload($class)
{
	$file[0] = $_SERVER['DOCUMENT_ROOT'].'/_global/classes/'.$class.'.php';
	$file[1] = $_SERVER['DOCUMENT_ROOT'].'/_global/models/'.$class.'.php';
	if (file_exists($file[0]))
	{
		require_once($file[0]);
	} elseif(file_exists($file[1])) {
		require_once($file[1]);
	} else {
		return false;
	}
}

// Works
$error = new Error();

// Included File
include('test.php');

test.php


// Does not work even if Bar.php exists.
$foo = new Bar();

Does anyone know what is causing this or has anyone had this problem before? I am developing on WAMP if that has any effect.

Thanks,
Darren

Try spl_autoload_register.
I (or rather, the framework I use) uses it, and I also have WAMP, and it works nicely :slight_smile:

Does test.php only have one line? If it’s only one line, then you need to include file.php in order for PHP to understand that an autoloader has been created.

You’re not doing anything with test.php by including it:


// Included File
include('test.php'); 

test.php file itself is being included in file.php and autoloader function is defined before the including line then where do you want to include the file.php file?

The code seems to work until there is no problem in configurations.