One of the coolest thing in php5 stuff is overloading. You can control access to your variable via __call, __get and __set methods. You may also make your variables readonly to other classes as shown below :

class SomeClass {
  /***********************************************************************************/
  /*                           -=Data Members=-                                      */
  /***********************************************************************************/
  private $variables = array();
  private $readOnlyVariables = array();
  /***********************************************************************************/
  public function __construct($application){
     $this->variables["MyReadonlyVariable1"] = -1;
     $this->variables["MyReadonlyVariable2"] = 'some value';
     $this->variables["MyVariable3"]         = 1;
     $this->variables["MyVariable4"]         = array();

     $this->readOnlyVariables["MyReadonlyVariable1"] = true;
     $this->readOnlyVariables["MyReadonlyVariable2"] = true;
  }
  /***********************************************************************************/
  private function __get($nm) {
     return $this->variables[$nm];
  }
  /***********************************************************************************/
  private function __set($nm, $val) {
     if(isset($this->readOnlyVariables[$nm])){
        throw new Exception("READONLY VARIABLE VIOLATION");
     }elseif(!isset($this->variables[$nm])){
        throw new Exception("REQUESTED VARIABLE DOESNT EXIST");
     }else {
        $this->variables[$nm] = $val;
     }
  }
  /***********************************************************************************/
  private function __isset($nm) {
     return isset($this->variables[$nm]);
  }

  private function __unset($nm) {
     if(isset($this->readOnlyVariables[$nm])){
        throw new Exception("READONLY VARIABLE VIOLATION");
     }else {
        unset($this->variables[$nm]);
     }
  }

  You may use your variables in $variables array as if they are normal data members in your SomeClass :

$someClass->MyReadonlyVariable1
$someClass->MyReadonlyVariable2
$someClass->MyVariable3
$someClass->MyVariable4

   You may also assign values to MyVariable3 and MyVariable4; but when you try to set value of MyReadonlyVariable1 and MyReadonlyVariable2 you will get an exception.

   In php 5, Class members can be overloaded to run custom code defined in your class by defining these specially named methods, and if user tries to set a value in your readonlyVariables list you can throw an exception not to allow that assignment.

Şu Yazıları da Sevebilirsiniz

Leave a Comment

* Bu formu kullanarak, verilerinizin bu web sitesi tarafından saklanması ve kullanılmasını kabul ediyorsunuz.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bu web sitesi deneyiminizi geliştirmek için çerezleri kullanır. Bunu kabul ettiğinizi varsayacağız, ancak isterseniz vazgeçebilirsiniz. Kabul Et Daha Fazla Bilgi