温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在php中对autoload机制进行加载

发布时间:2021-01-29 17:36:09 来源:亿速云 阅读:110 作者:Leah 栏目:开发技术

如何在php中对autoload机制进行加载?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1,自定义函数

2,spl_autoload_register()

复制代码 代码如下:


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

首先看自定义函数方式:

复制代码 代码如下:


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

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

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

运行结果如下:

复制代码 代码如下:


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
)

第二种方式:

复制代码 代码如下:


<?php
    class myLoader{
        public static function autoload($className){
            $filename = './include/'.$className.'.php';
            if(file_exists($filename)){
                include_once $filename;
            }else{
                exit('no file');
            }
        }
    }

    define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

    spl_autoload_register(array('myLoader', 'autoload'));

    /**
    *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
    * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
    */
    //spl_autoload_register( '__autoload' );

 
    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;
?>

运行结果如下:

复制代码 代码如下:


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

看完上述内容,你们掌握如何在php中对autoload机制进行加载的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI