zoukankan      html  css  js  c++  java
  • PHP 自动加载类

    <?php
    
    /**
     * 自动加载类文件
     * author: zbseoag
     * QQ: 617937424
     *
    //自动加载类名以'Model' 开头的类, 加载:./lib/Model/*.class.php 文件
    Autoload::home('./lib/')->match('Model')->suffix('class.php')->register()
    $user = new ModelUser();
    echo Autoload::$file;
    
    //测试文件路径
    echo Autoload::home(__DIR__)->make('Mysql');
    
    //注册再手动加载一个试
    Autoload::home(__DIR__)->register()->load('Mysql');
    
     *
     */
    
    class Autoload {
        
        public static $init;
        public $suffix = 'php';
        public $home = './';
        public static $file = '';
        public $match = '';
    
        private function __construct($home = ''){
    
            //删除 home 两边的斜杠: ModelUser
            $this->home = trim($home, '\/');
        }
    
        /**
         * 文件根目录
         * @param $value
         * @return $this
         */
    
        public static function home($value = ''){
    
            if(!self::$init) self::$init = new static($value);
            return self::$init;
        }
    
    
        /**
         * 匹配命名空间
         * @param $value
         * @return $this
         */
        public function match($value){
    
            $this->match = trim($value, '\/');
            return $this;
        }
    
        /**
         * 类名文件后缀,如 'class.php'
         * @param $value
         * @return $this
         */
        public function suffix($value){
            
            $this->suffix = ltrim($value, '.');
            return $this;
        }
    
        public function register(){
            spl_autoload_register(function($class){
                $this->load($class);
            });
    
            return $this;
        }
    
        /**
         * 生成文件路径
         * @param $class
         * @return string
         */
        public function make($class){
    
            //删除 class 两边的斜杠: ModelUser
            $class = trim($class, '\');
    
            self::$file = '';
            if(empty($this->match) || strpos($class, $this->match) === 0){
                self::$file = strtr($this->home . '/' . $class, '\',  '/') . '.' . $this->suffix;
            }
    
            return $this;
    
        }
    
        /**
         * 手动加载
         * @param $class
         */
        public function load($class = ''){
    
            if(!empty($class)) $this->make($class);
    
            if(self::$file) require self::$file;
            return $this;
        }
    
        /**
         * 动态创建对象
         * @param $class
         * @param $arguments
         * @return object
         * @throws ReflectionException
         */
        public static function create($class, $arguments){
    
            $class = new ReflectionClass($class);
            return $class->newInstanceArgs($arguments);
        }
    
    
        public function functions(){
            return spl_autoload_functions();
        }
    
    
        
    }
    

      

  • 相关阅读:
    zoj 3715 K
    bzoj 2002(弹飞绵羊) 分块
    最大01矩阵(悬线法)
    csu 1809 Parenthesis(线段树)
    csu 1804(有向无环图)
    csu 1803(2016)
    RCC 2017 Qual 1 Mail.Ru, April 2, 2017 Problem C. Magic Artifact
    Unmarshaller解析xml文件
    sax解析xml文件,封装到对象中
    cas环境搭建
  • 原文地址:https://www.cnblogs.com/zbseoag/p/12717214.html
Copyright © 2011-2022 走看看