__autoload difficulty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

__autoload difficulty

Post by gautamz07 »

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 ??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: __autoload difficulty

Post by requinix »

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).
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: __autoload difficulty

Post by gautamz07 »

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 :

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();
?>
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 :

Code: Select all

$temp_car = new car();
the autoload function is called

Code: Select all

function __autoload($class_name) 
{
   require_once ("$class_name.".'php');
}
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 ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: __autoload difficulty

Post by Celauran »

In this case it needs to be, because that's what you've told your autoloader to look for: $class_name.php
Post Reply