PHP Call a instance method with call_user_func within the same class

前端 未结 3 1451
深忆病人
深忆病人 2020-12-17 09:07

I\'m trying to use call_user_func to call a method from another method of the same object, e.g.

class MyClass
{
    public function __construct(         


        
3条回答
  •  天涯浪人
    2020-12-17 09:16

    This works for me:

    foo('bar');
        }
        public function foo($method)
        {
            return call_user_func(array($this, $method), 'Hello World');
        }
    
        public function bar($message)
        {
            echo $message;
        }
    }
    
    $mc = new MyClass();
    ?>
    

    This gets printed out:

    wraith:Downloads mwilliamson$ php userfunc_test.php 
        Hello World
    

提交回复
热议问题