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



  1. <?php
  2.   require_once('interface.Validator.php');
  3.  
  4.   abstract class PropertyObject implements Validator {
  5.  
  6.      protected $propertyTable = array();     // Przechowuje pary nazwa-warto__
  7.                                              // przypisujące w_asno_ci do
  8.                                              // pól bazy danych
  9.      protected $changedProperties = array(); // Lista w_asno_ci, które zosta_y
  10.                                              // zmodyfikowane
  11.  
  12.      protected $data;                        // Dane z bazy danych
  13.  
  14.      protected $errors = array();            // Ewentualne b__dy walidacji
  15.  
  16.      public function __construct($arData) {
  17.         $this->data = $arData;
  18.      }
  19.  
  20.      function __get($propertyName) {
  21.      if(!array_key_exists($propertyName, $this->propertyTable))
  22.         throw new Exception("B__dna w_asno__ \"$propertyName\"!");
  23.  
  24.      if(method_exists($this, 'get' . $propertyName)) {
  25.         return call_user_func(array($this, 'get' . $propertyName));
  26.      } else {
  27.         return $this->data[$this->propertyTable[$propertyName]];
  28.      }
  29.   }
  30.  
  31.   function __set($propertyName, $value) {
  32.      if(!array_key_exists($propertyName, $this->propertyTable))
  33.         throw new Exception("B__dna w_asno__ \"$propertyName\"!");
  34.      if(method_exists($this, 'set' . $propertyName)) {
  35.         return call_user_func(
  36.                               array($this, 'set' . $propertyName),
  37.                               $value
  38.                              );
  39.      } else {
  40.  
  41.         // Je_li warto__ w_asno_ci uleg_a zmianie i nie ma jej
  42.         // jeszcze w tabeli changedProperties, zostaje do niej do__czona
  43.         if($this->propertyTable[$propertyName] != $value &&
  44.            !in_array($propertyName, $this->changedProperties)) {
  45.            $this->changedProperties[] = $propertyName;
  46.         }
  47.  
  48.         // Przypisuje now_ warto__
  49.         $this->data[$this->propertyTable[$propertyName]] = $value;
  50.  
  51.         }
  52.      }
  53.  
  54.      function validate() {
  55.  
  56.      }
  57.  
  58.   }
  59. ?>



  1. <?php
  2.   require_once('class.PropertyObject.php');
  3.  
  4.   class Address extends PropertyObject {
  5.  
  6.      function __construct($addressid) {
  7.         $arData = DataManager::getAddressData($addressid);
  8.  
  9.         parent::__construct($arData);
  10.  
  11.         $this->propertyTable['addressid'] = 'adres_id';
  12.         $this->propertyTable['id'] = ' adres_id ';
  13.         $this->propertyTable['entityid'] = 'jednostka_id';
  14.         $this->propertyTable['address1'] = 'sadres1';
  15.         $this->propertyTable['address2'] = 'sadres2';
  16.         $this->propertyTable['city'] = 'smiasto';
  17.         $this->propertyTable['zipcode'] = 'skod';
  18.         $this->propertyTable['type'] = 'styp';
  19.      }
  20.  
  21.      function validate() {
  22.         if(strlen($this->zipcode) != 6) {
  23.            $this->errors['zipcode'] = 'Nale_y poda_ poprawny kod pocztowy.';
  24.         }
  25.  
  26.         if(!$this->address1) {
  27.            $this->errors['address1'] = 'Adres to pole wymagane.';
  28.         }
  29.  
  30.         if(!$this->city) {
  31.            $this->errors['city'] = 'Miasto to pole wymagane.';
  32.         }
  33.  
  34.         if(sizeof($this->errors)) {
  35.            return false;
  36.         } else {
  37.            return true;
  38.         }
  39.      }
  40.  
  41.      function __toString() {
  42.         return $this->address1 . ', ' .
  43.            $this->address2 . ', ' .
  44.            $this->city . ', ' .
  45.            $this->zipcode;
  46.      }
  47.   }
  48. ?>