PHP静态方法在成员变量中使用命名空间调用

Nat*_*per 5 php namespaces

是不是可以在PHP中做这样的事情?我想在一个成员变量中有一个名称空间,并且总是能够调用该类的每个静态方法,就像我在下面所做的那样.

当然我的代码不起作用,但我只是想知道这是否可行,并且我接近解决方案,或者如果这完全不可能并且必须始终使用语法:

\Stripe\Stripe::setApiKey(..);
Run Code Online (Sandbox Code Playgroud)

类似的问题澄清

注意:我无法修改Stripe类,重要的是它保持不变,因为未来的开发人员必须更新Stripe API

简化代码:

class StripeLib
{
    var $stripe;

    public function __construct()
    {
        // Put the namespace in a member variable
        $this->stripe = '\\'.Stripe.'\\'.Stripe;
    }
}

$s = new StripeLib();

// Call the static setApiKey method of the Stripe class in the Stripe namespace
$s->stripe::setApiKey(STRIPE_PRIVATE_KEY);
Run Code Online (Sandbox Code Playgroud)

Daa*_*aan 1

是的,这样的事情是可能的。有一个class可以调用的静态方法,它返回类的名称空间路径。

<?php
namespace Stripe;

Class Stripe {

    public static function setApiKey($key){
        return $key;    
    }

}

class StripeLib
{
    public $stripe;

    public function __construct()
    {
        // Put the namespace in a member variable
        $this->stripe = '\\'.Stripe::class;
    }
}

$s = (new StripeLib())->stripe;

// Call the static setApiKey method of the Stripe class in the Stripe namespace
echo $s::setApiKey("testKey"); //Returns testkey
Run Code Online (Sandbox Code Playgroud)