I would like to understand the "new" autoload of PHP [...]
New in parts. Autoload through spl_register_autoload
is available since the 5.1.2 of PHP (January 12, 2006). And even then, there are implementations of the function in the release of 5.1.0 .
__ autoload
Returning to the "core" of the issue. The [__autoload][4]
function was the first to exist in PHP and has existed since the release of the 5.0.0 version of PHP (13 July 2004).
Basically, you should create a function with the name __autoload
(similar to magic methods , but belonging to the global scope and not to a class).
Because it belongs to the global scope, you can only create a single autoload function. That is, all autoload rules should be contained in one place.
It's important to note that namespaces
was only implemented from PHP 5.3.0 . Between version 5.0.0 and 5.3.0, it was 5 years of development and a lot of speculation (I have literatures informing that separation between namespacess would be ::
instead of \
).
The problem
Early in the release of PHP 5, with the autoload news, many libraries also started using __autoload
. However, a limitation was noted with the use of namespace. An application that used __autoload
could not use a library that also used% with%. This was a major reason for conflict with third-party libraries.
spl_autoload_register
On the other hand, __autoload
allows you to "register" as many autoloads as you need. In this case, PHP will run each autoload (in the order they were registered, default Chain Of Responsibility in>) until you can load the class requests or all options are exhausted.
In this way, third-party libraries would no longer have problems implementing their own autoload. Because there were no rules defining how a class should be named and what file path it should have, it was quite common for each library to have its own convention of how to load a class.
Years later, definitions were known as PSR-0 , and some time later, PSR-4 , which normalized how a class should implement a spl_autoload_register
and be defined in a file path .
Even though having the PSRs, it is still possible to find differences between namespace
of different libraries, mainly the difference between namespaces that are omitted from the physical path ( file path ) and exist as logical namespaces (only in classes 0. You can check the link below:
Composer - Autoload and PSR-0 vs PSR-4
[...] the spl_autoload_register [...] where do I put it in my file? At the end of the autoload method?
Anywhere. The only detail is that the function, or class, that should be used as autoload should already exist.
So much so, that third-party libraries would load their own autoload only when executed / included in the project ( namespaces
/ require
). This issue ended up being handled with the use of autoload include
, which loads all autoloads together.
When and what to use?
Without a doubt, the answer will always be the same. If your PHP version is above 5.1.2, always use composer
. Otherwise, well, there is no choice, there is only spl_autoload_register
. Another detail is that the __autoload
function has been deprecated in PHP version 7.2.0.
link
Warning This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
As a tip, instead of creating your own autoload, I recommend using autoload generated by __autload
. Fast, easy and optimized, it will not let you down.