### 万能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];
}
~~~
- 序言
- 框架
- 框架获取
- 框架安装
- 应用结构
- 框架结构
- 应用目录结构
- 框架流程
- 命名规范
- 核心思想
- 扩展
- 类组装
- 框架基类
- Arr
- Arr扩展函数
- Date基类
- Dir
- Dir基类扩展函数
- Dom基类
- File基类
- Str基类
- Str随机字符
- Str操作函数
- Str随机创建
- Html基类
- Header基类
- Filter基类
- 框架核心
- 路由
- 路由信息
- 路由安装样本
- 路由逻辑原理
- 路由方法匹配
- 路由分组
- 路由构建
- 路由限制
- 路由规则
- 路由多语,多主题
- 路由常量
- 路由扩展
- 路由例
- 路由真静态使用
- 智能加载
- Response基类
- 定时任务
- 框架主类
- 控制器
- model
- model样本
- model使用
- view
- view路径
- view方法
- view配置
- view使用
- view真静态
- view例
- view多主题
- 模版引擎
- 模版引擎之单句解析
- 模版引擎之foreach解析
- cache
- cache注意事项
- cache配置
- cache方法
- cache使用
- cache扩展
- cache处理类File配置
- cache处理类Apc,Apcu配置
- cache处理类memcache,memcached配置
- cookie
- session
- 框架扩展
- Upload类
- 测试
- 测试流程
- 测试文件编写
- 测试文件例-数组篇
- 测试类函数
- 测试类使用
- 测试注意事项
- 常用功能
- 读码须知
- 个人技巧或习惯
- 万能get,call,callStatic
- &&和||
- 变量值交换
- trait拼接
- 多语支持