Php 如何在同一类中静态调用非静态方法(非严格模式)
我们有一个使用很多静态方法的库。然而,这使得在PHPUnit中进行测试变得困难,因为我无法模拟那些静态方法调用。因此,我正在删除静态关键字并逐步迁移静态调用,例如:Php 如何在同一类中静态调用非静态方法(非严格模式),php,phpunit,Php,Phpunit,我们有一个使用很多静态方法的库。然而,这使得在PHPUnit中进行测试变得困难,因为我无法模拟那些静态方法调用。因此,我正在删除静态关键字并逐步迁移静态调用,例如: $users = App_Model_User::find(...); // method declaration public static function find(...) {... // method calls within the class $result = static::find(...); …致: 这将
$users = App_Model_User::find(...);
// method declaration
public static function find(...) {...
// method calls within the class
$result = static::find(...);
…致:
这将允许我在测试时交换项目。但是,在模型库中,我们有许多静态定义的方法和调用,例如:
$users = App_Model_User::find(...);
// method declaration
public static function find(...) {...
// method calls within the class
$result = static::find(...);
我开始从方法声明中删除静态关键字,但我有点不确定如何声明方法调用。我试着用self
替换static
,但我不知道这是否是正确的用法。我不想使用$this,因为我们将处于一种状态,应用程序的某些部分正在静态调用find,而较新的部分或已更改的部分正在使用服务定位器:
public function someMethod()
{
$result = self::find(...); // is this correct? I want to be able to call find here statically as well as when an instance without breaking things
顺便说一下,我们不是在严格模式下运行的。我知道严格模式可能是个不错的选择。我从Laravel那里得到了鼓舞,因为我知道它能够静态调用相同的方法或作为实例方法:
User::where(...)->get();
// $user instance passed into the controller action
$user->where(...)->get();
…所以我希望我正在尝试做的不是太糟糕的练习,我希望。如果您对此有任何建议,我们将不胜感激