Jump to content

Question about using __get()


eldan88

Recommended Posts

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

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));
	}
}

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.