Sometimes the template definition needs to write a long full path class name, here is a simple alias calling code to circumvent this problem, the disadvantage is that the IDE code hints function is not
*//Initialize * Zgfacade::setzgfacade (' form ', ' aert_form '); * Zgfacade::setzgfacade (' esclient ', ' \elasticsearch\client '); * *//Use demo * $form = zgfacade::newinstance (' form ', ' frm2 ', ' delete '); * Dump ($form); * * echo zgfacade::server (' form ', ' request_method '); * Echo zgfacade::get (' form ', ' a '); * * $dsn = config::get (' Esken.dsn '); * $esClient = zgfacade::newinstance (' esclient ', $DSN); * Dump ($esClient); * * @author vb2005xu@qq.com */final class zgfacade{private static $map = [' form ' = ' aert_form '];p ublic static function s Etzgfacade ($alias, $class) {self:: $map [$alias] = $class;} private static function __facade__ ($facade, $method, $arguments =[]) {if (Is_object ($facade)) {//Call object method return Call_user_ Func_array ([$facade, $method], $arguments);} else if (is_string ($facade)) {if (Empty (self:: $map [$facade])) {throw new Exception ("Undefined ' facade ': {$facade}");} Call the static method if (' newinstance ' = = $method) {$class = new Reflectionclass (self:: $map [$facade]); Return $class->newinstanceargs ($arguments); } $class = self:: $map [$facade];return Call_user_func_array ([$class, $method], $arguments);} throw new Exception ("Invalid ' facade ' call!");} Public Function __call ($method, $arguments) {$facade = Array_shift ($arguments); Return self::__facade__ ($facade, $method, $arguments); The public static function __callstatic ($method, $arguments) {$facade = Array_shift ($arguments); Return self::__facade__ ($facade, $method, $arguments); }}
The above describes the facade wrapper class-to solve the long namespace call problem in the view, including the namespace, Acad aspects of the content, I hope to be interested in PHP tutorial friends helpful.