views:

158

answers:

4

I am creating a __get() function for a class to control access to my private member variables. Do I need to design the function to handle all possible member value reads or can I not write it for members that are public? Also, I am assuming that classes that inherit this class will use my __get() function to access private members.

class ClassA{
  private $collection = array();
  public $value;

  function __get($item){
    return $collection[$item];
  }
+1  A: 

No, you don't.


class A {
   public $foo = 'bar';
   private $private = array();

   public function __get($key) {
      echo 'Called __get() at line #' ,__LINE__, ' with key {', $key ,'}',"\n";
      return $this->private[$key];
   }

   public function __set($key, $val) {
      $this->private[$key] = $val;
   }
}


$a = new A();

var_dump($a->foo);
$a->bar = 'baz';
var_dump($a->bar);

And yes, it will:


class B extends A { private $private = array(); }
$b = new B();
var_dump($b->bar);
TML
A: 

Well, your code would fail on private items not set in your array. But then again, you can use this as a way to deal with what's in and out of your array, as such ;

  function __get($item){
    if ( isset ( $collection[$item] ) )
       return $collection[$item];
    else {
       try {
          return $this->$item ;  // Dynamically try public values
       } catch (Exception $e) {
          $collection[$item] = 0 ; // Make it exist
       }
    }
  }

Classes that inherit your calls will use this __get(), but can be overridden, so use parent::__construct() for explicity. Also note that these cannot be static. Further reading.

AlexanderJohannesen
I can handle that with overloading __isset().
tvanover
A: 

It's ok to call public variables of a class directly or by using some __get() kind of function

$x = new ClassA();
$x->value; // directly
$x->__get('key'); // or using __get() function

But it's better practice to use same approach to fetching public/private variables

Ish Kumar
A: 

First of all PHP searches for property name in class definition and tries to return its value. If there's no property - PHP tries to call __get($var) and here you can return anything you want. This is a little confusing behavior for those, who know Java-like getters/setters where you have to define them for every class member you want to access.

When it's comfortable to use Java-like getters/setters - you may write something like this:

public function __set($var, $value)
{
 if (method_exists($this, $method = "_set_" . $var))
 {
  call_user_func(array($this, $method), $value);
 }
}
public function __get($var)
{
 if (method_exists($this, $method = "_get_" . $var))
 {
  return call_user_func(array($this, $method), $value);
 }
}

and then use this code by defining custom getters/setters

protected function _get_myValue() 
     {
         return $this->_myValue; 
     }
protected function _set_myValue($value)
{
    $this->_myValue = $value;
}

and access to defined methods this way:

$obj->myValue = 'Hello world!';
Jet