Just wondering why something like this doesn’t work:
public function address($name){
if(!isset($this->addresses[$name])){
$address = new stdClass();
$address->city = function($class = '', $style = ''){
return $class;
};
$this->addresses[$name] = $address;
}
return $this->addresses[$name];
}
Calling it like echo $class->address('name')->city('Class') should just echo Class, however I get Fatal error: Call to undefined method stdClass::city()
I can find a better way to do this, because this will get messy, but I’m wondering what I might be doing wrong there, or if PHP doesn’t support this and why.
PHP is right when invoke fatal error
Call to undefined method stdClass::city()because object$class->address('name')has no methodcity.Intead, this object has property
citywhich is instance of Closure Class (http://www.php.net/manual/en/class.closure.php)You can verify this:
var_dump($class->address('name')->city)I found the way to call this anonymous function is:
Hope this helps!