【发布时间】:2016-10-09 09:33:34
【问题描述】:
我需要使用带有__construct() 方法的静态方法来实例化Client 对象,但据我所知,没有办法使用__construct(),因为使用静态方法时对象没有被实例化。
我认为我可以使用 init 方法。
class API
{
static $client;
public static function init()
{
$settings = [
'username' => 'user1',
];
self::$client = new Client($settings);
}
public static function foo( )
{
self::$client->action('Foo text');
}
}
API::init();
然后我可以在其他地方加载上面的类,然后做下面的事情。
API::foo();
我的问题:
- 我写课程的方式有什么问题吗?
- 以上代码是否会导致性能问题?
- 有没有更好的办法?
感谢任何帮助。
【问题讨论】:
标签: php class oop constructor static-methods