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.
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; }; }