爲了說明析構函數,如果該對象之前的值的變化被破壞下面的代碼塊被書中給出一個數據庫被更新的概念:解釋簡單的PHP代碼
<?php
class use {
private $_properties;
private $_changedProperties //Keeps a list of the properties that were altered
private $_hDB;
//_construct and __get omitted for brevity
function __set($propertyName, $value) {
if(!array_key_exists($propertyName, $this->_properties))
throw new Exception('Invalid property value!');
if(method_exists($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->_properties[$propertyName] !=$value && !in_array($propertyName, $this->_changedProperties)) {
$this->_changedProperties[] = $propertyName;
}
其餘部分的代碼是不必要的代碼和已被省略。請從這一點解釋代碼:
if(method_exists($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->_properties[$propertyName] !=$value && !in_array($propertyName, $this->_changedProperties)) {
$this->_changedProperties[] = $propertyName;
}
爲什麼我問這是我想驗證我對代碼的理解/理解。