سؤال

Looks like impossible do something like that:

class Service
{
    public function get($argument1, $argument2)
    {
        echo "Arguments: $argument1, $argument2.";
    }
}

class Main
{
    public function __callStatic($method, $arguments)
    {
        $method = "get".ucfirst($method);
        $arguments = implode(", ", $arguments);

        $object = self::$method;

        return $object->get($arguments);
    }

    public static function getService()
    {
        return new Service;
    }
}

$main = new Main;
$main::Service("1", "2"); // Should output 'Arguments: 1, 2.'

The problem is the self mess everthing, with this will be possible.

Use self::$variable, self this we have a variable in the Main context.

$object = self don't work too.

$object = clone self dont' work too.

define("method", $method); self::method will not work because self thinks is a class constant property.

this can't be used.

Is there a way to perform this?

هل كانت مفيدة؟

المحلول

Just pass the original $arguments directly to call_user_func_array:

return call_user_func_array(array($object, "get"), $arguments);

Then, just make a static call to Main:

Main::service("1", "2");

See it here in action: http://codepad.viper-7.com/phhqy6

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top