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); ?> Link to comment https://forums.phpfreaks.com/topic/289050-question-about-using-__get/ Share on other sites More sharing options...
kicken Posted June 7, 2014 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)); } } Link to comment https://forums.phpfreaks.com/topic/289050-question-about-using-__get/#findComment-1482166 Share on other sites More sharing options...
eldan88 Posted June 8, 2014 Author Share Posted June 8, 2014 Thanks kicken! Link to comment https://forums.phpfreaks.com/topic/289050-question-about-using-__get/#findComment-1482196 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.