Re: [PHP] Why do I have to declare __set if I declare __get for read-only properties?

2005-04-05 Thread Marek Kilimajer
C Drozdowski wrote:
Howdy,
I'd like to access some of the private members of my classes as 
read-only properties without resorting to function calls to access them. 
(e.g. $testClass-privateMember instead of $testClass-privateMember(), 
etc)

Based on my research and testing, using the __get and __set overloading 
methods appears to be the only way to do so. It also, based on testing, 
appears that these private members must be in an array.

What I do not understand is that if I declare a __get method I MUST also 
declare a do nothing __set method to prevent the read-only properties 
from being modified in code that uses the class.

For example, the code below allows me to have read-only properties. 
However, if I remove the do nothing __set method completely, then the 
properties are no longer read-only.

I'm curious as to why I HAVE to implement the __set method?
Example:
?php
class testClass
{
   private $varArray = array('one'='ONE', 'two'='TWO');
   public function __get($name)
   {
   if (array_key_exists($name, $this-varArray)) {
   return $this-varArray[$name];
   }
   }
   public function __set($name, $value)
   {
   }
}
$test = new testClass();
$test-one = 'TWO';   // doesn't work
echo $test-one;  // echo s 'ONE'
?
If __set function is not created, line $test-one = 'TWO'; creates a new 
public variable with name 'one' and this one is echoed on the next line, 
not the one retrieved from __get.

__get is called only for class variables that do not exist.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Why do I have to declare __set if I declare __get for read-only properties?

2005-04-04 Thread C Drozdowski
Howdy,
I'd like to access some of the private members of my classes as 
read-only properties without resorting to function calls to access 
them. (e.g. $testClass-privateMember instead of 
$testClass-privateMember(), etc)

Based on my research and testing, using the __get and __set overloading 
methods appears to be the only way to do so. It also, based on testing, 
appears that these private members must be in an array.

What I do not understand is that if I declare a __get method I MUST 
also declare a do nothing __set method to prevent the read-only 
properties from being modified in code that uses the class.

For example, the code below allows me to have read-only properties. 
However, if I remove the do nothing __set method completely, then the 
properties are no longer read-only.

I'm curious as to why I HAVE to implement the __set method?
Example:
?php
class testClass
{
   private $varArray = array('one'='ONE', 'two'='TWO');
   public function __get($name)
   {
   if (array_key_exists($name, $this-varArray)) {
   return $this-varArray[$name];
   }
   }
   public function __set($name, $value)
   {
   }
}
$test = new testClass();
$test-one = 'TWO';   // doesn't work
echo $test-one;  // echo s 'ONE'
?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php