我是新來的PHP和目前我正在閱讀Wrox專業PHP 5.PropertyObject Class Help
任何人都可以解釋我下面的代碼?
<? 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;
}
}
}
?>
我無法理解評估器中的代碼get和set方法。
謝謝,我明白了,但你能告訴我什麼是array_key_exits,call_user_func和in_array呢?我試圖在谷歌搜索這些語法,但沒有在我的腦海中。 – Searock 2010-08-08 18:41:15
請參閱http://php.net/array_key_exists,http://php.net/call_user_func和http://php.net/in_array – Artefacto 2010-08-08 18:43:27
+1謝謝,解釋看起來很簡單。 – Searock 2010-08-08 18:47:15