Reputation: 3581
I have index.php file where on the very top I define __autoload magic function.
function __autoload($className) {
echo "autoloading $className";
require_once('application/models/'.strtolower($className).'.php');
}
Then, I reference to the User object that is included by __autoload automatically (User.php). Everything works fine until this moment. The problem is that User.php references to the File class
$obj->numberOfUploads = File::countUploadsByUser($obj->ID);
and at this point the error is thrown
Fatal error: Class 'File' not found in Z:\home\project\www\application\models\user.php on line 19
The thing is, that i can see the output
autoloading User
but cannot see anything regarging the File. So, as it seems, the __autoload function is not called at all when the reference to File is caught. Tryting to add __autoload function to the User.php resulted in an error about redeclaring the function.
I am sorry if the question was raised before, I tried to google it and found no information.
Cheers
Upvotes: 1
Views: 125
Reputation: 4536
From the comments on autoload in the php manual it seems like a number of people have had similar problems when it comes to defining __autoload
twice.
I would recommend using spl_autoload_register
with an anonymous function to avoid multiple definition conflicts, as illustrated in example 1 of that page:
// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
Also, you don't need to use require_once
(can just use require
) because if the class is already found, the handler won't be called again.
Try using spl_autoload_register
just in your index.php
file, and if that doesn't work, put one in your User.php
as well.
Best of luck!
Upvotes: 3