Detailed explanation of examples of autoload mechanism for automatic loading of PHP custom functions

伊谢尔伦
Release: 2023-03-12 08:50:02
Original
1432 people have browsed it

This article mainly introduces the example of PHP autoload autoload mechanism. The file structure is as follows. There are two ways to achieve automatic loading. Friends in need can refer to

1, Custom function

2, spl_autoload_register()

The code is as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan  800 Feb 19 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan  906 Feb 19 11:28 ./spl_autoload.php
./include:
total 16
drwxrwxr-x 2 liuyuan liuyuan 4096 Feb 19 11:42 ./
drwxrwxr-x 3 liuyuan liuyuan 4096 Feb 19 11:43 ../
-rw-rw-r-- 1 liuyuan liuyuan  142 Feb 19 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan  143 Feb 19 11:42 bClass.php
Copy after login

First look at the custom function method:

The code is as follows:

<?php
    define(&#39;EOL&#39;, (PHP_SAPI == &#39;cli&#39;) ? PHP_EOL : &#39;</ br>&#39;);
    print_r(get_included_files());
    echo EOL;
    print get_include_path();
    echo EOL;
    //set_include_path(get_include_path().PATH_SEPARATOR.&#39;/var/www/ly_php/php_spl/include/&#39;);
    //set_include_path(dirname(FILE).&#39;/include&#39;);
    //set_include_path(dirname(FILE).&#39;/include/&#39;);

    function autoload($className){
        $filename = &#39;./include/&#39;.$className.&#39;.php&#39;;
        //$filename = &#39;./include/&#39;.$className.&#39;.php&#39;;
        //$filename = &#39;/var/www/ly_php/php_spl/include/&#39;.$className.&#39;.php&#39;;
        if(file_exists($filename)){
            include_once $filename;
        }else{
            exit(&#39;no file&#39;);
        }
    }

    $a = new aClass();
    $b = new bClass();
    print_r(get_included_files());
?>
Copy after login

The running result is as follows:

The code is as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php func_autoload.php 
Array
(
    [0] => /var/www/phpgcs/php_autoload/func_autoload.php
)
.:/usr/share/php:/usr/share/pear
aClass is loaded
bClass is loaded
Array
(
    [0] => /var/www/phpgcs/php_autoload/func_autoload.php
    [1] => /var/www/phpgcs/php_autoload/include/aClass.php
    [2] => /var/www/phpgcs/php_autoload/include/bClass.php
)
Copy after login

Second type Method:

The code is as follows:

<?php
    class myLoader{
        public static function autoload($className){
            $filename = &#39;./include/&#39;.$className.&#39;.php&#39;;
            if(file_exists($filename)){
                include_once $filename;
            }else{
                exit(&#39;no file&#39;);
            }
        }
    }
    define(&#39;EOL&#39;, (PHP_SAPI == &#39;cli&#39;) ? PHP_EOL : &#39;<br />&#39;);
    spl_autoload_register(array(&#39;myLoader&#39;, &#39;autoload&#39;));
    /**
    *autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
    * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
    */
    //spl_autoload_register( &#39;autoload&#39; );
 
    
error_reporting
(E_ALL^E_NOTICE^E_WARNING^E_ERROR);
    error_reporting(E_NOTICE | E_WARNING );
    $a = new aClass();
    print_r(get_included_files());
    echo EOL;
    $b = new bClass();
    echo EOL;
?>
Copy after login

The running result is as follows:

The code is as follows:

liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php spl_autoload.php 
aClass is loaded
Array
(
    [0] => /var/www/phpgcs/php_autoload/spl_autoload.php
    [1] => /var/www/phpgcs/php_autoload/include/aClass.php
)
bClass is loaded
Copy after login


The above is the detailed content of Detailed explanation of examples of autoload mechanism for automatic loading of PHP custom functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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