Using The __autoload() Function

If we are stuck on PHP 5.0 for some reason, we can use the __autoload() function in place of the SPL autoloader registry. There are drawbacks to doing things this way, but under PHP 5.0 it is our only alternative. We do not need to register it with SPL (in fact, we cannot, since SPL was not introduced until PHP 5.1). We will not be able to mix-and-match other autoloaders in this implementation; only one __autoload() function is allowed. If an __autoload() function is already defined, we will need to merge this code with any code already existing in the function:

setup.php 1 <?php 2 // ... setup code ... 3 4 // define an __autoload() function 5 function __autoload($class) 6 { 7 // ... the global function code ... ...

Get Modernizing Legacy Applications in PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.