Php PropertyObject类帮助
我是php新手,目前正在阅读Wrox Professional PHP5 谁能给我解释一下下面的代码吗Php PropertyObject类帮助,php,Php,我是php新手,目前正在阅读Wrox Professional PHP5 谁能给我解释一下下面的代码吗 <? php abstract class PropertyObject { //Stores name/value pairs that hook properties to database field names protected $propertyTable=array(); //List of properties that have been modified. pro
<? php
abstract class PropertyObject
{
//Stores name/value pairs that hook properties to database field names
protected $propertyTable=array();
//List of properties that have been modified.
protected $changedProperties=array();
//Actual data from the database.
protected $data;
//Any validation errors that might have occured.
protected $errors=array();
public function __construct($arData)
{
$this->data=$arData;
}
function __get($propertyName)
{
if(!array_key_exits($propertyName,$this->propertyTable))
{
throw new Exception("Invalid property \"$propertyName\" !");
}
if(method_exists($this,'get'.$propertyName))
{
return call_user_func(array($this,'get'.$propertyName));
}
else
{
return $this->data[$this->propertyTable[$propertyName]];
}
}
function __set($propertyName,$value)
{
if(!array_key_exits($propertyName,$this->propertyTable))
{
throw new Exception("Invalid property \"$propertyName\" !")
}
if(method_exits($this,'set'.$propertyName))
{
return call_user_func(array($this,'set'.$propertyName),$value);
}
else
{
//If the value of the property really has changed and it's not already in the changedProperties array, add it.
if($this->propertyTable[$propertyName] !=$value && !in_array($propertyName,$this->changedProperties))
{
$this->changedProperties[]=$propertyName;
}
//Now set the new value
$this->data[$this->propertyTable[$propertyName]]=$value;
}
}
}
?>
当请求对象的属性但未声明或指定(用于动态属性)该属性时,将调用\u get
魔术方法。这一实施:
- 首先尝试查看逻辑属性是否作为名为
$propertyTable
的实际声明属性中的条目存在
- 如果它不存在,则抛出异常,因此保留该方法
- 如果它存在并且另外存在一个名为
'get.$propertyName
(即,“get”
与请求属性名连接)的方法,则调用该方法并返回其值
- 如果它存在但没有这样的方法,它将返回声明的属性
$propertyTable
中键为$propertyName
的条目的值
考虑到这一点,我认为您可以计算出\u set
。请参阅PHP手册。这是设置DB存储类的一种非常常见的方法。发生的事情是基于PropertyObject实例化一个对象(因为PropertyObject是抽象的)
它继承了\uu get()
和\uu set()
方法。无论何时通过->
操作符访问对象的数据,都会分别调用\uuuu get()
和\uu set()
方法
$m->foo; #calls MyObject::__get('foo');
$m->bar = 'baz'; #calls MyObject::__set('bar','baz');
\uuu get()
方法首先检查属性表中是否定义了键(此处为DB中的字段建模),如果不存在,则抛出异常。
然后,get()。因此,假设foo
是propertyTable
中的一个键,\uu get()
将查看我们是否定义了一个方法getfoo
,如果定义了,则为我们调用它,并返回其值
//if(method_exists($this,'get'.$propertyName))
//{
// return call_user_func(array($this,'get'.$propertyName));
//}
$m->foo; # checks if MyObj::getfoo is defined, and if so, calls it
最后,如果propertyTable
中有一个键foo
,但没有名为getfoo
的方法,那么它只需返回$m->data
中的数组位置值,其键是propertyTable
中的数组位置值,其键是foo
\uuu set()
的定义方式大致相同,但不是返回存储在数据
数组中的值,而是检查是否有预先设置的“set”,并检查对象上设置的值是否与数据
数组中的值有任何不同,如果是,在设置新值之前,将属性名称添加到changedProperties数组中。谢谢,我知道了,但是您能告诉我数组\u key\u存在什么,调用\u user\u func和in\u array做什么吗?我试着在谷歌上搜索这些语法,但我的大脑里什么都没有。看到了吗
//if(method_exists($this,'get'.$propertyName))
//{
// return call_user_func(array($this,'get'.$propertyName));
//}
$m->foo; # checks if MyObj::getfoo is defined, and if so, calls it