我一直在寻找如何在我自己的类中访问条带静态方法。基本上扩展了条带库。
在Stripes文档中,代码如下所示:
\Stripe\Customer::create();
我试图从条带库访问的代码:
public static function create($params = null, $opts = null)
{
return self::_create($params, $opts);
}
当前文件顶部:
use Stripe\Charge;
use Stripe\Stripe;
use Stripe\Customer;
/**
* Handles all stripe specific options
*/
class _Stripe
{
protected $charge;
protected $stripe;
protected $customer;
/**
* Init Stripe Charge
*/
public function init_charge()
{
$this->charge = new Charge;
}
/**
* Init Stripe Class
*/
public function init_stripe()
{
$this->stripe = new Stripe;
}
/**
* Init Stripe Customer
*/
public function init_customer()
{
$this->customer = new Customer;
}
到目前为止,我有这个:
$this->customer = new Customer;
$this->customer::create();
被认为是无效的语法。在我自己的课堂上,有什么方法可以让我这样做吗?
我尝试过的事情:
{$this->customer}::create();
Got: Unexpected '}'
提前致谢!
答案 0 :(得分:1)
这可以在PHP7中使用(我不确定其他版本):
<?php
class Test
{
public static $var1 = "something";
public static function charge()
{
return self::$var1;
}
}
$Object = new Test();
echo $Object->charge(); // Output: something
当您更新问题时,我使用您的结构进行模拟,下面的代码适用于PHP 7.0.5-3:
<?php
namespace Stripe;
class Customer
{
public static function create($params = null, $opts = null)
{
return self::_create($params, $opts);
}
public static function _create($params, $opts)
{
return 'bla bla';
}
}
// ...
use Stripe\Customer;
$Customer = new Customer();
echo $Customer->create(null, null); // Output: bla bla
// ...
class OtherClass
{
public $instance;
public function run()
{
$this->instance = new Customer();
echo $this->instance->create(null, null);
}
}
(new OtherClass)->run();