0

I am reviving some old code that hasn't been used for some time and see that there is a depreciated function which, when I saw it, I thought was a custom function so was surprised that it could be depreciated but I came to realize that the underscores have some meaning. It was __autoload() and I did not write this code (a former colleague did) but I was able to fix the issue by reviewing a posting found elsewhere on this site so my question is mainly for the information.

My question is, what are the underscores for and what does this function actually do? It appears to load any function it finds in a file folder but this seems an odd thing to do. What could be the purpose?

This is the original giving the error

function __autoload($class){
    $class = strtolower($class);
    $fName = jBASE . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class .'.php';
    $classFName = jBASE . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'class.'. $class .'.php';
    if(file_exists($fName)):
        require_once $fName;
    elseif(file_exists($classFName)):
        require_once($classFName);
    endif;
}

and this is the version after I repaired it which gives no error

spl_autoload_register(function($class){    
    $class = strtolower($class);
    $fName = jBASE . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class .'.php';
    $classFName = jBASE . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'class.'. $class .'.php';
    if(file_exists($fName)):
        require_once $fName;
    elseif(file_exists($classFName)):
        require_once($classFName);
    endif;
});
DonP
  • 725
  • 1
  • 8
  • 27
  • 1
    Does this answer your question? [php spl\_autoload\_register vs \_\_autoload?](https://stackoverflow.com/questions/6894538/php-spl-autoload-register-vs-autoload) – shingo Sep 21 '20 at 07:31
  • I had actually noticed those before postings and while they help a bit to get me to this point, they do not really answer my specific questions. What exactly is being auto-loaded and how does it work? What are the underscores for? Are they an actual part of the function name or do they have some other significance? I'm not a beginner but this is something new to me that I wish to understand in case it might be useful elsewhere. – DonP Sep 22 '20 at 06:48
  • 1
    The underscores are just part of the [function name](https://www.php.net/manual/en/function.autoload.php). – Álvaro González Sep 22 '20 at 14:58
  • Thank you! The person who programmed the site used lots of single and double underscores to differentiate between custom functions with otherwise the same name as standard ones so you can see why I was confused. So it seems that `__autoload()` is a depreciated PHP function which is also being created here as a custom function with `function __autoload() {}` so how is that possible to create a function when one with the same name already exists in PHP? – DonP Sep 22 '20 at 18:31
  • 1
    Documentation says: "**You can define this function** to enable classes autoloading." (emphasis mine). It's like an interface. If PHP knew how to load your classes, it wouldn't need such a function in the first place. – Álvaro González Sep 24 '20 at 07:51
  • And, incidentally, that's why it was eventually deprecated: you cannot have two functions with the same name, so only one of the project libraries could use the feature. – Álvaro González Sep 27 '20 at 08:45

0 Answers0