在 class 变量中访问静态方法
accessing static method within class variable
我一直在四处寻找,试图找出如何在我自己的 class 中访问条带静态方法。实质上是扩展了 stripe 库。
在 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();
这被认为是无效语法。我有什么办法可以在我自己的 class?
中做到这一点
我尝试过的事情:
{$this->customer}::create();
Got: Unexpected '}'
提前致谢!
这个在 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();
我一直在四处寻找,试图找出如何在我自己的 class 中访问条带静态方法。实质上是扩展了 stripe 库。
在 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();
这被认为是无效语法。我有什么办法可以在我自己的 class?
中做到这一点我尝试过的事情:
{$this->customer}::create();
Got: Unexpected '}'
提前致谢!
这个在 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();