[PHP] __get() accessor question (a bug maybe?)

2006-11-15 Thread TemuriI

Hi all, here is the code I am having problem with:



class baseForm {
   private $_id = 10;

   function __get($member)
   {
   echo "Accessing member \"{$member}\" : \n";
   return $this->$member;
   }
   function checkID()
   {
   echo $this->_id."\n";
   }
}
class inputText extends baseForm
{
   function display()
   {
   $this->checkID();
   echo $this->_id."\n";
   }
}
$f = new inputText();
echo $f -> display();





The question is: why the string 'Accessing member "_id" :' is only 
displayed once? If you look at the code - it actually accesses $_id 
member TWICE. But __get() gets gets triggered only once. WHY?!!


From what I see, the __get() accessor function is triggerred only from 
OUTSIDE the class.


PHP Version 5.1.6, installed on Win32 (unfortunately).

Thank you in advance,
Temuri

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: __get() accessor question (a bug maybe?)

2006-11-15 Thread TemuriI

Hi, thanks for the response.

But the thing with __get() & _set() is that the members should either be 
*not* declared or be of private type to trigger accessor functions. 
That's the reason I have _id as private (also to trigger some other 
action when __set() fires up).


Now, it gets inherited by class inputText so I'd expect accessors work 
just the same in a class that extends any base class.


Or am I wrong there?

Thx,
Temuri


Barry wrote:

TemuriI schrieb:

Hi all, here is the code I am having problem with:



class baseForm {
   private $_id = 10;

   function __get($member)
   {
   echo "Accessing member \"{$member}\" : \n";
   return $this->$member;
   }
   function checkID()
   {
   echo $this->_id."\n";
   }
}
class inputText extends baseForm
{
   function display()
   {
   $this->checkID();
   echo $this->_id."\n";
   }
}
$f = new inputText();
echo $f -> display();





Probably bcause its private and therefore only for that class?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: __get() accessor question (a bug maybe?)

2006-11-15 Thread TemuriI

Barry,

that did not work.

Look, you said that $_id is of type private and that's why it cannot be 
accessed. However, I am trying to access it from checkID() that is a 
method of the same base class.


Any clue why it does not work?

Thx,
Temuri


Barry wrote:

TemuriI schrieb:
just a small idea.

public function __get() {...}

Might work *untested*



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] __get() accessor question (a bug maybe?)

2006-11-15 Thread TemuriI

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 = 'type="text"'.$this->nameHTMLStr.$this->idHTMLStr.$this->valueHTMLStr.$this->titleHTMLStr.$this->cssclassHTMLStr.' 
/>';
  if ($this->wrap_formfields_in_div) $this->wrap_in_tag('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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php