The Little Guy Posted February 24, 2011 Share Posted February 24, 2011 __call is used when a method has been called, is there anything similar to __call but for properties? I wanted to use __get, but that is only for private properties... Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/ Share on other sites More sharing options...
AbraCadaver Posted February 24, 2011 Share Posted February 24, 2011 __call() is not for methods, only for inaccessible methods just like __get() is for inaccessible properties, so no. Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179287 Share on other sites More sharing options...
The Little Guy Posted February 24, 2011 Author Share Posted February 24, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179312 Share on other sites More sharing options...
tibberous Posted February 24, 2011 Share Posted February 24, 2011 Use functions. Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179315 Share on other sites More sharing options...
The Little Guy Posted February 25, 2011 Author Share Posted February 25, 2011 Use functions. How would that work? Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179389 Share on other sites More sharing options...
trq Posted February 25, 2011 Share Posted February 25, 2011 I wanted to use __get, but that is only for private properties... If the properties are public they are already available. Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179418 Share on other sites More sharing options...
The Little Guy Posted February 25, 2011 Author Share Posted February 25, 2011 That I know, but in my example you can see my property is in class b, but I would like to call it from class A. How can I access it from class A? like this: echo $a->property_from_class_b; Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179567 Share on other sites More sharing options...
The Little Guy Posted February 25, 2011 Author Share Posted February 25, 2011 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/228739-__call-for-properties/#findComment-1179695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.