Declaring an anonymous function within new stdClass

风流意气都作罢 提交于 2020-01-14 07:40:28

问题


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.


回答1:


PHP is right when invoke fatal error Call to undefined method stdClass::city() because object $class->address('name') has no method city. Intead, this object has property city which 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:

$closure = $class->address('name')->city;
$closure('class');

Hope this helps!




回答2:


Sadly it is not possible within stdClass, but there is a workaround -- PHP Anonymous Object.

// define by passing in constructor
$anonim_obj = new AnObj(array(
    "foo" => function() { echo "foo"; }, 
    "bar" => function($bar) { echo $bar; } 
));

$anonim_obj->foo(); // prints "foo"
$anonim_obj->bar("hello, world"); // prints "hello, world"



回答3:


AFAIK, this is not supported by PHP, and you must use the call_user_func() or call_user_func_array() functions to call closures assigned to class properties (usually you can use __call() to do this, but in your case, the class is stdClass, so this isn't possible).



来源:https://stackoverflow.com/questions/9355377/declaring-an-anonymous-function-within-new-stdclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!