如果存在非静态函数,__ callStatic不会调用

时间:2015-06-25 00:36:51

标签: php

我的代码是这样的:

//OAuth2 Module configs
$authProvider.configure([ {
    "default": {
        apiUrl:                  API_URL,
        tokenValidationPath:     '/me',
        signOutUrl:              '/oauth',
        emailRegistrationPath:   '/oauth',
        accountUpdatePath:       '/oauth',
        accountDeletePath:       '/oauth',
        confirmationSuccessUrl:  window.location.href,
        passwordResetPath:       '/oauth',
        passwordUpdatePath:      '/oauth',
        passwordResetSuccessUrl: window.location.href,
        emailSignInPath:         '/oauth',
        forceHardRedirect: true,
        storage:                 'localStorage',
        proxyIf:                 function() { return false; },
        proxyUrl:                'proxy',
        authProviderPaths: {
            github:   '/auth/github',
            facebook: '/auth/facebook',
            google:   '/auth/google'
        },
        tokenFormat: {
            "access_token" : "{{ token }}",
            "token_type"   : "Bearer",
            "refresh_token": "{{ clientId }}",
            "expires_in"   : "{{ expiry }}",
            "scope"        : "{{ uid }}"
        },
        parseExpiry: function(headers) {
            var expires_in = parseInt(headers['expires_in'], 10) || null;
                return expires_in;
            },
            handleLoginResponse: function(response) {
                //Patch for persistant data as library retreive auth data from header.
                return response;
            },
            handleAccountResponse: function(response) {
                return response;
            },
            handleTokenValidationResponse: function(response) {
                return response;
            }
        }
} ]);

但它会回应:

<?php

class A {

    public function CallA()
    {
        echo "callA" . PHP_EOL;
    }

    public static function CallB()
    {
        echo "callB" . PHP_EOL;
    }

    public static function __callStatic($method, $args)
    {
        echo "callStatic {$method}";
    }
}

A::CallA();

也就是说,Strict Standards: Non-static method A::CallA() should not be called statically in /vagrant/hades_install/public/test.php on line 21 callA 不会遇到函数CallA

如果我希望使用__callStatic

调用__callStatic该怎么办?

2 个答案:

答案 0 :(得分:4)

正如documentation所解释的那样:

  在静态上下文中调用不可访问的方法时会触发

__callStatic()

代码中的方法CallA() 可访问,这就是PHP不使用__callStatic()并直接调用CallA()的唯一选择。

您可以强制调用__callStatic(),方法是CallA()无法访问(重命名或将其可见性更改为protectedprivate)或直接调用(丑陋)解决方法):

A::__callStatic('CallA', array());

如果您选择CallA()受保护,则需要实施方法__call()才能再次致电CallA()

class A {

    protected function CallA()
    {
        echo "callA" . PHP_EOL;
    }

    public static function CallB()
    {
        echo "callB" . PHP_EOL;
    }

    public static function __callStatic($method, $args)
    {
        echo "callStatic {$method}" . PHP_EOL;
    }

    public function __call($method, $args)
    {
        if ($method == 'CallA') {
            $this->CallA();
        }
    }
}

A::CallA();
A::__callStatic('CallA', array());

$x = new A();
$x->CallA();

输出:

callStatic CallA
callStatic CallA
callA

答案 1 :(得分:2)

另一种方法是保持非静态方法不变,并在静态调用时使用前缀,解析__callStatic中的方法名称。

class A {

    public function CallA()
    {
        echo "callA" . PHP_EOL;
    }

    public static function __callStatic($method, $args)
    {
        $method_name = ltrim($method, '_');
        echo "callStatic {$method_name}" . PHP_EOL;

        $instance = new A();
        return instance->$method_name(...$args); //this only works in PHP 5.6+, for earlier versions use call_user_func_array
    }

}

A::_CallA();

这将输出:

callStatic callA
callA