Re: __get() accessor question (a bug maybe?)

From: Date: Wed, 15 Nov 2006 16:44:40 +0000
Subject: Re: __get() accessor question (a bug maybe?)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-244682@lists.php.net to get a copy of this message
Hi Edward, What I am trying to do is to write a set of classes that generate HTML code for HTML form elements (input type=text, textarea, hidden, buttons, etc). Now I thought I'd define a base class that would have properties common to all form elements, like: value, name, id, title, css class and so on. But without actual generation of HTML elements. Sub-classes would extend that base class and actually generate HTML string. For example: =================== class HTMLformElement { private $_name; private $_id; private $_value; private $_title; public $nameHTMLStr; public $idHTMLStr; public $valueHTMLStr; public $titleHTMLStr; function __get($member) {
      if (strlen($this->$member))
      {
         return $this->$member;
      } else {
         return false;
      }
} function __set($member, $value) {
       $this->$member = htmlentities($value);
       $tmpStr = substr($member, 1)."HTMLStr";
       switch ($member)
       {
          case "_name":
            $this->$tmpStr = " name=\"{$this->$member}\"";
          break;
          case "_id":
            $this->$tmpStr = " id=\"{$this->$member}\"";
          break;
          case "_value":
             if ($this->who == 'HTMLtextarea')
                $this->$tmpStr = $this->$member;
             else
               $this->$tmpStr = " value=\"{$this->$member}\"";
          break;
          case "_title":
            $this->$tmpStr = " title=\"{$this->$member}\"";
          break;
       }
} } class HTMLtext extends HTMLformElement { function display() {
      $str = '';
      $strEl = '<input type="text"'.$this->nameHTMLStr.$this->idHTMLStr.$this->valueHTMLStr.$this->titleHTMLStr.$this->cssclassHTMLStr.' />';
      if ($this->wrap_formfields_in_div) $this->wrap_in_tag('<div class="'.$this->formelCSSClass.'">', $strEl);
      $str = $strEl . chr(10);
      return $str;
} } ================================== Now, the main idea behind the code above is in __set() accessor. I wanted to generate HTML tag attributes automatically, whenever a respective value is set: $HTMLtext->_name = "firstname" would automatically create a string inside __set() method: $HTMLtext->titleHTMLStr = ' name="firstname"'; Does that make sense to you or do you think that using accessors is not a good choice in my case? Thanks, Temuri Edward Kay wrote:
From what I see, the __get() accessor function is triggerred only from OUTSIDE the class.
From my understanding of OOP, that is how it should behave. __get and __set methods are there to abstract away the actual member variables and provide a consistent interface to other classes. When calling member variables from within the same class, they should be accessed directly. Edward


« previous php.general (#244682) next »