Home > Backend Development > PHP Tutorial > How to use spl_autoload_register() and __autoload() functions in PHP?

How to use spl_autoload_register() and __autoload() functions in PHP?

WBOY
Release: 2023-04-10 18:38:01
Original
3131 people have browsed it

In the previous article, I brought you "How to determine whether an object belongs to a class in PHP? ", which details the relevant knowledge that should be used to determine whether an object belongs to a class in PHP. In this article, we will take a look at the automatic loading mechanism in PHP. I hope everyone has to help!

How to use spl_autoload_register() and __autoload() functions in PHP?

In daily development and use, the basic idea of ​​our object-oriented programming is that it is usually customary to create a separate PHP source file for each class. In this case It provides convenience for subsequent maintenance and makes it easy to reuse classes.

In PHP, the automatic loading function of the class can be realized through the spl_autoload_register() and __autoload() functions, which can save our programming time and energy. Then let’s introduce these two functions separately.

<span style="font-size: 20px;"><strong><span style="font-size: 18px;">__autoload()</span></strong></span>##Function

__autoload() function is a magic method to be precise. We will take you to understand the magic method in PHP in five minutes (detailed examples)》Introduces some commonly used magic methods in detail. It is mentioned that it is automatically called, that is, the function needs to be called under specific conditions. When we create a new class, if the class cannot be found in the current source file, PHP will automatically call the

__autoload()

function and pass the class name to __autoload() function. This is the specific condition for the __autoload() function to be called. Its syntax format is as follows: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">function __autoload($class){ // 方法体 }</pre><div class="contentsignin">Copy after login</div></div>What we need to pay attention to is:

  • $class

    is the name of the class to be loaded.

  • __autoload()

    The function can only be defined once in the current source file.

  • If you want to use the
  • __autoload()

    function to automatically load a class file, the name of the class file needs to be the same as the class name. Only one class can be defined in another class file .

  • Next let’s look at the use of the __autoload() function through an example. The example is as follows:
<?php
    function __autoload($class){
        $file = &#39;./&#39;.$class.&#39;.php&#39;;
        include_once($file);
    }
    $obj = new Demo();
?>
Copy after login

Run the above code, it will automatically load the files in the same directory. Demo.php file, the code in Demo.php is as follows:

<?php
    class Demo{
    }
?>
Copy after login

What we need to pay attention to is: __autoload() function has been deprecated since PHP7.2.0, you can use spl_autoload_register() function instead .

<span style="font-size: 20px;">spl_autoload_register()<strong><span style="font-size: 18px;"></span></strong></span>## Functionspl_autoload_register() function can specify a function to replace the function of __autoload() function,

spl_autoload_register([$autoload_function [, $throw = true [, $prepend = false ]]])
Copy after login

What needs to be noted is:

    $autoload_function
  • : The function name to replace the __autoload() function, or it can be an anonymous function. If no parameters are provided, the default implementation function spl_autoload() of autoload is automatically registered;

  • $throw
  • : used to set $autoload_function. When the registration cannot be successful, spl_autoload_register () Whether the function throws an exception;

  • $prepend
  • : If it is true, the spl_autoload_register() function will add the $autoload_function function to the head of the queue, otherwise it will be added to the end of the queue.

    Next let’s take a look at an example. The example is as follows:
  • <?php
        spl_autoload_register(&#39;loadClass&#39;);
        function loadClass($class){
            $file = &#39;./&#39;.$class.&#39;.php&#39;;
            include_once($file);
        }
        $obj = new Demo();
    ?>
    Copy after login
    In the above example, the spl_autoload_register() function is used to specify another function to replace the __autoload() function. .

    If you are interested, you can click on "

    PHP Video Tutorial

    " to learn more about PHP knowledge.

    The above is the detailed content of How to use spl_autoload_register() and __autoload() functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template