【发布时间】:2016-03-21 08:04:34
【问题描述】:
有可能在 php 中做这样的事情吗?我想在成员变量中有一个命名空间,并且总是能够像下面那样调用该类的每个静态方法。
当然我的代码不起作用,但我只是想知道这是否可能并且我已经接近解决方案,或者这完全不可能并且必须始终使用语法:
\Stripe\Stripe::setApiKey(..);
Similar question for clarifications
注意:我无法修改 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);
【问题讨论】:
标签: php namespaces