简体   繁体   中英

Invoke doesn't seem to work on ReflectionMethod with empty array?

I have the following code:

$loClass    = new ReflectionClass('SampleClass');
$loMethod   = $loClass->getMethod('doSomething');

$loMethod->invoke(array());

Let say the class looks like this:

<?php

/**
 * Sample Class
 */
class SampleClass
{
    public function __construct()
    {

    }

    /**
     * Do somehting
     */
    public function doSomething($paParams)
    {
        // do something
    }
}

I get the following error:

object(ReflectionException)#181 (7) { ["message":protected]=> string(29) "Non-object passed to Invoke()", ..

What I want to do is call a class method from string with parameters as an array (can be empty array) Can any one help me out?

Unless the method is static you need to pass the object instance where to call this method:

$loClass    = new ReflectionClass('SampleClass');
$loMethod   = $loClass->getMethod('doSomething');
$sample     = new SampleClass();

$loMethod->invoke($sample, array());

Check the manual page of ReflectionMethod::invoke()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM