Yii2 理解Object

简介: 版本继承与实现构造函数 __construct__get __setmethod_exists__isset __unset其余判断函数1 版本// yii\BaseYii\getVersionpublic static function getVersion(){ return '2.0.10';}2 继

1 版本

// yii\BaseYii\getVersion
public static function getVersion()
{
    return '2.0.10';
}

2 继承与实现

Object实现了Configurable接口。
Configureable要求在构造函数的参数末尾加上$config

public function __constructor($param1, $param2, ..., $config = [])

3 构造函数 __construct

// 按照Configureable要求, 需要在参数末尾添加 $config = []
public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    // 调用了初始化函数
    $this->init();
}

4 __get, __set

// 重写了php5中预定义的__get
// php5中的代码:
public function __get($name)
{ 
    return $this->$name; 
} 
// yii中的代码
public function __get($name)
{
    // 由原来的直接调用属性改为通过调用get函数来间接调用
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) 
    {
        return $this->$getter();
    } 
    elseif (method_exists($this, 'set' . $name)) 
    {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    } 
    else 
    {
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
    }
}

同理于__set
在代码中可以看见,如果找不到$getter, 却找到了setter, 就会报出异常,这个属性不可读。

5 method_exists

用于检测类中指定的函数是否存在

6 __isset, __unset

__isset 用于判断某个属性是否定义了
__unset 用于将某个属性设置为null,但是对只读的属性,会报异常

最好不要直接使用__isset和__unset,而是使用isset和unset

public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) 
    {
        return $this->$getter() !== null;
    } 
    else 
    {
        return false;
    }
}
public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) 
    {
        $this->$setter(null);
    } 
    elseif (method_exists($this, 'get' . $name)) 
    {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

7 其余判断函数

以下函数基本上借助于method_exists和property_exists

hasProperty
canGetProperty
canSetProperty
hasMethod
相关文章
|
3天前
|
人工智能 安全 测试技术
|
5天前
|
云安全 人工智能 安全
阿里云 Agentic SOC 位居 IDC MarketScape安全运营智能体2026领导者类别
以 Agentic AI 重构安全运营闭环,阿里云云安全在产品能力与市场份额
1181 3
|
6天前
|
缓存 UED 开发者
Codex109天重置23次,明天还要再送一次
Codex近109天完成23次额度重置,7月14日将迎来第24次。Tibo高频响应用户反馈:优化GPT-5.6高消耗问题、补发失效福利、调整重置时间——形成“反馈→回应→修复→补偿”正向闭环,彰显以用户为中心的产品哲学。(239字)
691 11
|
5天前
|
数据采集 机器学习/深度学习 人工智能
田间杂草定位与检测4200张YOLO智慧农业数据集分享
本数据集含4200张真实农田图像,YOLO格式,单类别(杂草)高质量标注,覆盖多作物、多光照、多生长阶段等复杂场景,专为智慧农业杂草检测与智能除草设备研发设计,支持YOLOv5/v8/v10等主流模型训练。
365 94
|
9天前
|
存储 人工智能 JSON
Qwen 本地部署搭配 ComfyUI 生成 AI 漫剧完整实操指南(小白零基础可落地,零成本无限生成+角色一致性天花板)
2026全网最优本地漫剧流水线:零成本、离线运行、角色统一、低配(8G显卡)可跑。融合Qwen本地大模型+ComfyUI双引擎,实现剧本生成→分镜绘图→动态成片全自动,隐私安全、无审核限流,新手30分钟上手,日更无忧。(239字)
|
3天前
|
Web App开发 数据采集 人工智能
|
10天前
|
人工智能 缓存 JSON
刚刚 GPT-5.6 发布,吊打 Claude 5 和 Grok 4.5?一手实测来啦!
GPT-5.6 刚发布,跑分号称超越 Fable 5,真的假的?附一手实测,让 3 个最强 AI 编程模型 GPT-5.6 Sol、Claude Fable 5、Grok 4.5 在 Cursor 里同时一把梭开发网页游戏,看看最新模型到底谁更能打。
387 0
|
10天前
|
人工智能 IDE 开发工具
零门槛上手阿里云Qoder CN(原灵码):免费社区版、Credits额度与核心功能完整说明
Qoder CN(原通义灵码)是阿里云推出的AI智能编码助手,覆盖个人与企业全场景开发需求,提供免费社区版与付费专业版,引入Credits资源计费机制,支持多模型自由切换,适配主流开发环境,大幅提升编码效率。以下从版本权益、Credits计费、AI模型支持、核心功能及使用要点,全面解析Qoder CN的使用规则与能力边界,帮助用户精准选型、高效使用。
528 0