How can YiluPHP directly use all classes without configuration or injection?

Keywords: Programming PHP less Attribute

Friends who have used YiluPHP have found that no matter model class, logical class, auxiliary class or tool class, you do not need to set load or injection in the configuration file or use include or require or use in the page when you use all classes. Just use $app - > class name - > method name (). This mechanism is so convenient that everyone who has just started to use it is OK I will be a little confused about what I have done wrong? I'll tell you now that you haven't done anything less or done anything wrong. YiluPHP is designed in this way. Let me talk about how YiluPHP is designed.

Some people may want to use the SPL autoload register() function to do this. SPL autoload register() function can register any number of autoloaders. For example, in a third-party library, if you want to find a class, you need to traverse all autoloaders. The efficiency is very low, which is not in line with the principle of YiluPHP's pursuit of speed. The $app of YiluPHP is a global variable, which is an instance of a class named YiluPHP. This class uses php's magic method \u get. The code is as follows:
 
    public function __get($name)
    {
        if (isset($this->helper[$name])) {
            return $this->helper[$name];
        }
        $fun = $this->autoload_class;
        $class_name = $fun($name);
        unset($fun);
        if ($class_name!==false){
            $this->helper[$name] = new $class_name;
            return $this->helper[$name];
        }
        throw new Exception($this->lang('class_not_found').$name);
    }

When using $app - > class name - > method name (), it will first find out whether there is a corresponding class instance from the helper property of $app (the helper property is a container, which contains all the used class instances, so when the same class is used for the second time, it will not look up the file, nor do the instantiation operation. It will return directly from the helper container. The helper container's The design is also one of the reasons for the rapid operation of YiluPHP). If the corresponding class instance cannot be found in the container, the autoload class() function will be called to find the file. The autoload class() function is assigned to an attribute of $app, and its implementation is in the constructor of $app \,

    public function __construct()
    {
        $this->autoload_class = function ($class_name){
            $file = $GLOBALS['project_root'].'helper/'.$class_name.'.php';
            if (file_exists($file)) {
                //The file name, class name of helper class file and the calling method in app should be consistent
                require_once($file);
                return $class_name;
            }

            //Divide hump names with underscores
            $path = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $class_name);
            $path = explode('_', $path, 2);
            $path = $path[0].'/'.$class_name;
            $file = $GLOBALS['project_root'].$path.'.php';
            if (file_exists($file)) {
                //The file name, class name and calling method in app of class file should be consistent
                require_once($file);
                return $class_name;
            }

            //Aliasing of classes is supported
            if(!empty($GLOBALS['config']['helper_alias']) && array_key_exists($class_name, $GLOBALS['config']['helper_alias']) ){
                $real_class_name = $GLOBALS['config']['helper_alias'][$class_name];
                $file = $GLOBALS['project_root'].'helper/'.$real_class_name.'.php';
                if (file_exists($file)) {
                    require_once($file);
                    return $real_class_name;
                }

                //Divide hump names with underscores
                $path = preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $real_class_name);
                $path = explode('_', $path, 2);
                $path = $path[0].'/'.$real_class_name;
                $file = $GLOBALS['project_root'].$path.'.php';
                if (file_exists($file)) {
                    require_once($file);
                    return $real_class_name;
                }
            }
            return false;
        };
    }
The autoload ﹣ u class() function first searches for class files in the helper directory. If it cannot be found, it searches for class files in the corresponding directory according to the prefix word of the class name (the process of searching according to the alias of the class is also omitted in the middle). If it cannot be found, an exception of "class not found" will be thrown. In fact, the first author intends to store all the class files in the helper directory, which is to reduce the program execution process and improve the running speed of the framework. However, considering the following two reasons, it is decided to support the division of directory storage classes:
 
First, if there are too many system functions and class files, it is difficult to locate class files when writing code. Although when there are too many system functions, the best choice is to split the large system into small systems and deploy them separately to disperse pressure, many entrepreneurial companies are prone to too many files due to the rapid change of demand;
 
Second, storing class files in different directories according to different functions has become a structured thinking habit of almost all programmers. Conforming to this habit can make programmers adapt to the Yilu PHP framework faster.
In addition to keeping the habit of storing by directory, there are also naming habits that are compatible with the two naming rules of "underline connection word" and "Hump". Therefore, a model class can be named either model Uuser or model user automatically.
 
The following is the flow chart of a class's method being called
 

Posted by mxdan on Fri, 20 Dec 2019 03:56:07 -0800