美文网首页
PHP最佳实践——自动加载类

PHP最佳实践——自动加载类

作者: ozil_oo | 来源:发表于2018-09-13 11:50 被阅读0次

    使用[spl_autoload_register()](http://php.net/manual/en/function.spl-autoload-register.php)来注册你的自动加载函数

    <?php
    // First, define your auto-load function.
    function MyAutoload($className){
        include_once $className . '.php';
    }
     
    // Next, register it with PHP.
    spl_autoload_register('MyAutoload');
     
    // Try it out!
    // Since we haven't included a file defining the MyClass object, our auto-loader will kick in and include MyClass.php.
    // For this example, assume the MyClass class is defined in the MyClass.php file.
    $var = new MyClass();
    ?>
    

    相关文章

      网友评论

          本文标题:PHP最佳实践——自动加载类

          本文链接:https://www.haomeiwen.com/subject/uxhwgftx.html