call_user_func within class context (with $this defined)

前端 未结 4 380
野趣味
野趣味 2020-12-21 01:09

Is there a way how to execute closure in PHP5.3 within a context of an object?

class Test {
    public $name=\'John\';

    function greet(){
        eval(\'         


        
4条回答
  •  情歌与酒
    2020-12-21 01:24

    what about:

    class Test {
        public $name='John';
    
        function greet(){
            eval('echo "Hello, ".$this->name;');
    
            call_user_func(function($obj){
                echo "Goodbye, ".$obj->name;
            }, $this);
        }
    }
    $c = new Test;
    $c->greet();
    

提交回复
热议问题