zoukankan      html  css  js  c++  java
  • PHP 类的命名空间 和自动载入

    PHP 类的自动载入有两种方法,__autoload() 和 spl_autoload_register() ,就是在PHP代码中new一个类的时候,会自动触发,将类的类名包括命名空间作为参数传进入方法里,在方法里可根据命名空间和类名准确找到类文件,从而require或者inlcude进来。菜鸟一枚,作为备忘

    <?php
    function auto($class){
            //$class = ABE;
            /** 命名空间的自动载入 **/
            $class_path = explode("\",$class);
            $file = __DIR__ . '/' ;
            foreach($class_path as $c){
               $file .= $c . '/';
            }
            $file = rtrim($file,"/");
            $file .= '.php';
            var_dump($file);exit;
    }
    spl_autoload_register('auto');
    use ABE;
    $e = new E();
    echo 'hi';
    /*******输出*******/
    string(32) "/www/test_php_autoload/A/B/E.php"
  • 相关阅读:
    自适应行高
    IOS各类问题
    KVC
    数据模型的构建及懒加载数据
    NSBundle
    九宫格布局获取行/列索引
    QLineEdit
    QLabel
    排序算法
    SpringBoot配置文件-yaml
  • 原文地址:https://www.cnblogs.com/undefined-j/p/10399374.html
Copyright © 2011-2022 走看看