💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 万能get,call,callStatic * * * * * 原理: > call和callStatic同理,只不过返回一个函数 > ~~~ > return function ($arg1, $arg2) { > ..code > //return $arg1 > $arg2; > }; > ~~~ ~~~ 目录结构: ├──... 仓库, 放东西的, 需要对应权限 │ ├──gets 万能get | | └──test.php 一个A类test属性 │ └──A.php A类session ~~~ **A.php:** ~~~ <?php class a { /** * 万能get * * @param string $property 属性 * @example $property = 'test' * @return miexd */ public function __get(string $property) { //获得过的属性 static gets = []; //如果不存在 //!isset($gets['test']) if (!isset($gets[$property])) { //载入当前目录下gets目录下$property.php //载入文件 当前目录/gets/test.php,返回值为test,此时$gets[$property] ----> 'test' $gets[$property] = require __DIR__ . DIRECTORY_SEPARATOR.'gets'.DIRECTORY_SEPARATOR.$property.'.php'; } //返回值 return $gets[$property]; } } ~~~ **test.php:** ~~~ <?php return 'test'; ~~~ **使用:** ~~~ $a = new A(); echo $a->test; ---->'test' ~~~ * * * * * **万能call框架版本:** *....vendor/msqphp/fraemwork/traits/Get.php* ~~~ <?php declare(strict_types = 1); namespace msqphp\traits; trait Get { /** * 万能get * * @param string $property 属性 * * @return miexd */ public function __get(string $property) { //get集合 static $gets = []; //如果属性不存在 if (!isset($gets[$property])) { //框架路径 $framework_path = dirname(__DIR__) . DIRECTORY_SEPARATOR; //命名空间转换为目录 msqphp\base\dir\Dir ----> base\dir $namespace = strtr(str_replace([strrchr(__CLASS__, '\\'), 'msqphp\\'], '', __CLASS__), '\\', DIRECTORY_SEPARATOR); //拼装路径 $file = $framework_path . $namespace . DIRECTORY_SEPARATOR . 'gets' . DIRECTORY_SEPARATOR . $property . '.php'; //不存在则替换 is_file($file) || $file = str_replace($framework_path, \msqphp\Environment::getPath('library'), $file); //存在载入否则报错 if (!is_file($file)) { throw new TraitsException(__CLASS__.'类的'.$property.'属性不存在'); } $gets[$property] = require $file; } return $gets[$property]; } ~~~