Yii Object对象与属性相关的方法

来源:互联网 发布:西门子300plc编程实例 编辑:程序博客网 时间:2024/06/05 22:50
以下内容文字来源于请猛戳这里,对于学习Yii本人认为十分有帮助,代码来自于Yii源码!!!!

__isset() 用于测试属性值是否不为 null ,在 isset($object->property) 时被自动调用。 注意该属性要有相应的getter

public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {//判断是否有该属性对应的getter方法
return $this->$getter() !== null;//返回该属性是否等于null的结果
} else {
return false;//如果该没有改属性对应的getter方法,返回false
}
}

hasProperty() 用于测试是否有某个属性。即,定义了getter或setter。 如果 hasProperty() 的参数$checkVars = true (默认为true), 那么只要具有同名的成员变量也认为具有该属性

public function hasProperty($name, $checkVars = true)
{

return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

canGetProperty() 测试一个属性是否可读,参数 $checkVars 的意义同上。只要定义了getter,属性即可读。 同时,如果 $checkVars 为 true 。那么只要类定义了成员变量,不管是public, private 还是 protected, 都认为是可读。

public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

canSetProperty() 测试一个属性是否可写,参数 $checkVars 的意义同上。只要定义了setter,属性即可写。 同时,在 $checkVars 为 ture 。那么只要类定义了成员变量,不管是public, private 还是 protected, 都认为是可写。

public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}



0 0
原创粉丝点击