50

According to http://php.net/manual/en/language.oop5.autoload.php the magic function __autoload() was deprecated as of PHP 7.2.0 and removed as of PHP 8.0.0.

The official alternative is spl_autoload(). See http://www.php.net/manual/en/function.spl-autoload.php. But the PHP manual does not explain the proper use of this baby.

My question: How to replace this (my automatic class autoloader) with a version with spl_autoload()?

function __autoload($class) {
    include 'classes/' . $class . '.class.php';
}

Problem: I cannot figure out how to give that function a path (it only accepts namespaces).

By the way, there are a lot of threads regarding this topic here on SO, but none gives a clean & simple solution that replaces my sexy one-liner.

hakre
  • 193,403
  • 52
  • 435
  • 836
Sliq
  • 15,937
  • 27
  • 110
  • 143
  • 2
    I've added a basic example, based on the example above, into the PHP documentation. Hopefully things like this won't be so hard to find in future. – salathe May 22 '12 at 20:53
  • PHP manual doesn't suck at all. – Shikiryu Feb 24 '13 at 18:34
  • 1
    @Shikiryu Well, to be honest, it does. In most cases it's not easy to understand how something works, because the docs have been written by total hardcore nerds. Even when you know how something works, it's not possible to understand the offical docs. To make it worse, in most cases the official docs are corrected (!) and rewritten by a hell lot of comments. Things like mysqli, cookies or sessions are very hard to find and much harder to understand. When you do it like in the examples you will fail. That's a general problem in IT & open source: no usability. – Sliq Feb 25 '13 at 09:59
  • I was totally honest. The doc seems clear to me. For example, for this particular question, the chosen answer was all written in the "Tip" section on the page *you* gave… But now, I just saw you asked almost a year ago so it may have changed. – Shikiryu Feb 25 '13 at 10:12

1 Answers1

83

You need to register autoload functions with spl_autoload_register. You need to provide a "callable". The nicest way of doing this, from 5.3 onwards, is with an anonymous function:

spl_autoload_register(function($class) {
    include 'classes/' . $class . '.class.php';
});

The principal advantage of this against __autoload is of course that you can call spl_autoload_register multiple times, whereas __autoload (like any function) can only be defined once. If you have modular code, this would be a significant drawback.


2018 update to this: there shouldn't really be that many occasions when you need to roll your own autoloader. There is a widely accepted standard (called PSR-4) and several conforming implementations. The obvious way of doing this is using Composer.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 3
    Its also worth mentioning it would probably be a good idea to make youre autoloading be [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) which is pretty much the standard (in addition to legacy support for the old PEAR conventions). If you are writing your own autoloader this makes it pretty compatible with what some of the larger and popular frameworks are doing. If you dont want to write your own but use the naming/storage conventions specified you can use an existing autoloader component. – prodigitalson May 21 '12 at 15:18
  • @lonesomeday Thanks, this is a perfect solution that replaces the above autoload() thing 100%. – Sliq May 21 '12 at 15:45
  • answer complement [here](https://pt.stackoverflow.com/questions/292270/fatal-error-cannot-redeclare-spl-autoload-register) – Leffa Aug 01 '22 at 02:36