"New" autoload from PHP 7

Asked

Viewed 5,136 times

4

I would like to understand the "new" autoload of PHP, in it I can choose the name I want to put?

For example, in the old one, it was __autoload. That in case, I can put autoload_de_teste ?

Another question is what spl_autoload_register does and where do I put it in my file? At the end of the autoload method?

Old autoload:

function __autoload($Class){
    $P = ['Conn'];
    $Incluido = null;

    foreach ($P as $Pasta) {
        if(!$Incluido && file_exists(__DIR__ . "\\{$Pasta}\\{$Class}.class.php") && !is_dir(__DIR__ . "\\{$Pasta}\\{$Class}.class.php")):
            include_once __DIR__ . "\\{$Pasta}\\{$Class}.class.php";
            $Incluido = true;
        else:
            trigger_error("Erro ao incluir: " . __DIR__ . "\\{$Pasta}\\{$Class}.class.php");
            die;
        endif;
    }
}

The New would look like this, for example:

function autoload_de_teste($Class){
    $P = ['Conn'];
    $Incluido = null;

    foreach ($P as $Pasta) {
        if(!$Incluido && file_exists(__DIR__ . "\\{$Pasta}\\{$Class}.class.php") && !is_dir(__DIR__ . "\\{$Pasta}\\{$Class}.class.php")):
            include_once __DIR__ . "\\{$Pasta}\\{$Class}.class.php";
            $Incluido = true;
        else:
            trigger_error("Erro ao incluir: " . __DIR__ . "\\{$Pasta}\\{$Class}.class.php");
            die;
        endif;
    }
}

spl_autoload_register("autoload_de_teste");

This is the doubt in the case, the function of the spl_autoload_register and where I should put it, (for example, beginning of the file, or at the end of the autolod method, etc...) and if in this way of doing, if I can put any name.

1 answer

6

I would like to understand the "new" autoload of PHP[...]

New in parts. Autoload through function spl_register_autoload is available since version 5.1.2 of PHP (January 12, 2006). And, even so, there are implementations of the function in the release of the version 5.1.0.

__autoload

Back to the "core" of the question. The function [__autoload][4] was the first to exist in PHP and has existed since the release of the 5.0.0 of PHP (Thursday, 13 July 2004).

Basically, you should create a function with the name __autoload (similar to magical methods, but belonging to the global scope and not to a class).

Since 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 is important to note that namespaces was only implemented from the version 5.3.0 of PHP. Between version 5.0.0 and 5.3.0, it was 5 years of development and many speculations (I have literature stating that separation between namespacess would be :: instead of \).

The problem

Right at the beginning of the release of PHP 5, with the novelty of autoload, many libraries have also used __autoload. However, a limitation was perceived with the use of namespace. An application that used the __autoload could not use a library who also used the __autoload. This was a major cause of conflict with third-party libraries (third party).

spl_autoload_register

On the other hand, spl_autoload_register allows how many are "registered" autoloads are needed. In this case, PHP will run each autoload (in the order in which they were registered, standard Chain Of Responsibility) until you can load the requested class or all options are exhausted.

This way, third-party libraries would no longer have problems implementing their own autoload. Because there were no rules that defined how a class should be named and what file path should have, it was quite common for each library to have its own convention of how to load a class.

Years later, there was the definition known as PSR-0 and some time later, PSR-4, which normalized the way a class should implement a namespace and be defined in a file path.

Even so, possessing the PSR’s, it is still possible to find differences between namespace of different libraries, mainly the difference between namespace which are omitted from the physical path (file path) and exist as namespace logical (only in the classes 0. Which you can check in the link below:

Composer - Autoload and PSR-0 vs PSR-4

[...]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, which is to be used as autoload must already exist.

So much so that third-party libraries carried their own autoload only when executed/included in the project (require/include). This issue was eventually dealt with using the autoload of commiserate, who carries all the autoloads together.

When and what to use?

No doubt, the answer will always be the same. If your PHP version is 5.1.2 or higher, always use spl_autoload_register. Otherwise, well, there’s no choice, there’s just __autoload. Another detail is that the function __autload was discontinued (deprecated) in PHP version 7.2.0.

http://php.net/manual/en/function.autoload.php

Warning This Feature has been DEPRECATED as of PHP 7.2.0. Relying on this Feature is highly discouraged.

As a hint, instead of creating your own autoload, recommend the use of autoload generated by commiserate. Fast, easy and optimized, doesn’t leave you hanging.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.