Witam,
Mam problem ze zrozumieniem kodu. Prosze o wytłumaczenie.
1) W którym momencie wywoływana jest funkcja __get() i w którym __set(). Jeśli dobrze zrozumialem to w momencie wywolania
$this->propertyTable['addressid'] = 'adres_id';
2) Prosiłbym o opisanie działania if(!array_key_exists($propertyName, $this->propertyTable)) bo nie jestem pewien czy dobrze to rozumiem.
3) Tej lini nie rozumię kompletnie if(method_exists($this, 'get' . $propertyName)) {
return call_user_func(array($this, 'get' . $propertyName));
} else {
return $this->data[$this->propertyTable[$propertyName]];
}
'get'.$propertyName to łączenie stringów ale nie rozumiem w jakim celu to jest robione. Jak by ktoś opisał byłbym wdzięczny
<?php
require_once('interface.Validator.php');
abstract class PropertyObject implements Validator {
protected
$propertyTable = array(); // Przechowuje pary nazwa-warto__ // przypisujące w_asno_ci do
// pól bazy danych
protected
$changedProperties = array(); // Lista w_asno_ci, które zosta_y // zmodyfikowane
protected $data; // Dane z bazy danych
protected
$errors = array(); // Ewentualne b__dy walidacji
public function __construct($arData) {
$this->data = $arData;
}
function __get($propertyName) {
throw new Exception("B__dna w_asno__ \"$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) {
throw new Exception("B__dna w_asno__ \"$propertyName\"!");
if(method_exists($this, 'set' . $propertyName)) {
return call_user_func(
array($this, 'set' . $propertyName), $value
);
} else {
// Je_li warto__ w_asno_ci uleg_a zmianie i nie ma jej
// jeszcze w tabeli changedProperties, zostaje do niej do__czona
if($this->propertyTable[$propertyName] != $value &&
!in_array($propertyName, $this->changedProperties)) { $this->changedProperties[] = $propertyName;
}
// Przypisuje now_ warto__
$this->data[$this->propertyTable[$propertyName]] = $value;
}
}
function validate() {
}
}
?>
<?php
require_once('class.PropertyObject.php');
class Address extends PropertyObject {
function __construct($addressid) {
$arData = DataManager::getAddressData($addressid);
parent::__construct($arData);
$this->propertyTable['addressid'] = 'adres_id';
$this->propertyTable['id'] = ' adres_id ';
$this->propertyTable['entityid'] = 'jednostka_id';
$this->propertyTable['address1'] = 'sadres1';
$this->propertyTable['address2'] = 'sadres2';
$this->propertyTable['city'] = 'smiasto';
$this->propertyTable['zipcode'] = 'skod';
$this->propertyTable['type'] = 'styp';
}
function validate() {
if(strlen($this->zipcode) != 6
) { $this->errors['zipcode'] = 'Nale_y poda_ poprawny kod pocztowy.';
}
if(!$this->address1) {
$this->errors['address1'] = 'Adres to pole wymagane.';
}
if(!$this->city) {
$this->errors['city'] = 'Miasto to pole wymagane.';
}
return false;
} else {
return true;
}
}
function __toString() {
return $this->address1 . ', ' .
$this->address2 . ', ' .
$this->city . ', ' .
$this->zipcode;
}
}
?>