was just reading this tutorial and also saw this thread :
tutorial : http://www.programmerinterview.com/inde ... -function/
Thread : http://stackoverflow.com/questions/1270 ... n-php-code
WHile using __autoload the class name has to be exactly the same as the file name ??
__autoload difficulty
Moderator: General Moderators
Re: __autoload difficulty
No, but it generally is because that's the easiest way to deal with autoloading. I've worked with a third-party framework that stored classes all over the freaking codebase and it uses the autoloader to look up class names in an array of filenames. Which is stupid because it's entirely non-obvious where a particular class lives.
By the way, use spl_autoload_register instead of __autoload. Still the same overall concept but it allows multiple autoloaders (since you can't have multiple "__autoload" functions).
By the way, use spl_autoload_register instead of __autoload. Still the same overall concept but it allows multiple autoloaders (since you can't have multiple "__autoload" functions).
Re: __autoload difficulty
Thanks ! yeah i saw people say __autoload is depreciated and stuff and spl_autoload or spl_autoload_register is the one to be used , but , i'll slowly graduate into all of that , my aim of leaning autoload was to have a basic idea of how composer etc works , i still have a scrambled brain about all this , but anyways , i even build my own custom programe so i could understand autoload :
https://github.com/gautamz07/autoload
BTW , checkout my index page , it has the following code :
now if i go the the car.php file and rename the class car to carr ,
an error is thrown in my index.php file , which brings me to the following conclusion :
when i say the following line :
the autoload function is called
now since i have changed the name of the class car to carr , but still kept the file name car.php ,
autoload will throw an error because i am guessing its actually checking for a file name called carr.php , which does exist .
so my conclusion is :
The file name has to be the same as the class name . am i wrong ?
https://github.com/gautamz07/autoload
BTW , checkout my index page , it has the following code :
Code: Select all
<?php
function __autoload($class_name)
{
require_once ("$class_name.".'php');
}
$temp = new vehicle();
echo "<br>";
$temp_car = new car();
echo "<br>";
$temp_bus = new bus();
?>an error is thrown in my index.php file , which brings me to the following conclusion :
when i say the following line :
Code: Select all
$temp_car = new car();Code: Select all
function __autoload($class_name)
{
require_once ("$class_name.".'php');
}autoload will throw an error because i am guessing its actually checking for a file name called carr.php , which does exist .
so my conclusion is :
The file name has to be the same as the class name . am i wrong ?
Re: __autoload difficulty
In this case it needs to be, because that's what you've told your autoloader to look for: $class_name.php