eldan88 Posted June 7, 2014 Share Posted June 7, 2014 Hey Guys. I am trying to return a method by using __get() with the code below and it seems to be returning "NULL" Below is my code Any help would be really appreciated! <?php class Person { function __get($property){ $method = "get{$property}"; if(method_exists($this, $method)) { return $this->$method; } } function getName(){ return "Bob"; } function getAge(){ return 44; } } $p = new Person(); var_dump($p->Age); ?> Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted June 7, 2014 Solution Share Posted June 7, 2014 If you want to execute the method and return it's return value, you should use call_user_func or call_user_func_array. function __get($property){ $method = "get{$property}"; if(method_exists($this, $method)) { return call_user_func(array($this, $method)); } } Quote Link to comment Share on other sites More sharing options...
eldan88 Posted June 8, 2014 Author Share Posted June 8, 2014 Thanks kicken! Quote Link to comment 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.