Home > Backend Development > PHP Tutorial > php __callStatic方法参数的一个问题

php __callStatic方法参数的一个问题

WBOY
Release: 2016-06-06 20:38:05
Original
968 people have browsed it

看下面的代码

<code>php</code><code><br>class XXOO
{
    private static $instance;

    public function func($arg1, $arg2, $arg3, $arg4)
    {

    }

    public static function __callStatic($method, $arguments)
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance->$method($arguments);
    }
}

XXOO::func('arg', 'arg2', array('name'), 'sf');
</code>
Copy after login
Copy after login

__callStatic实现静态方法调用实例方法,但是$arguments变量是一个数组,在不改变func方法(参数可能不固定)的前提下如何实现对实例方法传参.

回复内容:

看下面的代码

<code>php</code><code><br>class XXOO
{
    private static $instance;

    public function func($arg1, $arg2, $arg3, $arg4)
    {

    }

    public static function __callStatic($method, $arguments)
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance->$method($arguments);
    }
}

XXOO::func('arg', 'arg2', array('name'), 'sf');
</code>
Copy after login
Copy after login

__callStatic实现静态方法调用实例方法,但是$arguments变量是一个数组,在不改变func方法(参数可能不固定)的前提下如何实现对实例方法传参.

<code>call_user_func_array([self::$instance, $method], $arguments);
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template