Jump to content

Question about using __get()


Go to solution Solved by kicken,

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

  • Solution

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));
	}
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.