Jump to content

__call for properties


The Little Guy

Recommended Posts

Doing this to get a method works, I would like to do something like this to get a property as well.

class A{
private $b;
public function __construct(){
	$this->b = new B();
}
public function __call($m, $a){
	if(method_exists($this->b, $m)){
		$this->b->$method();
	}
}
}

class B{
public $property = 'Class B Property<br>';
public function methodB(){
	return "Method B<br>";
}
}

$a = new A();
// Works:
echo $a->methodB();
// Doesn't work:
echo $a->property;

 

What can I do?

	public function __get($name) {
	if(property_exists($this->magic, $name)){
		$refl = new ReflectionObject($this->magic);
		$prop = $refl->getProperty($name);
		if($prop->isPublic()){
			return $this->magic->$name;
		}
	}
}
public function __set($name, $value){
	if(property_exists($this->magic, $name)){
		$refl = new ReflectionObject($this->magic);
		$prop = $refl->getProperty($name);
		if($prop->isPublic()){
			$this->magic->$name = $value;
		}
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.