在php中减少$this伪变量的使用?

Reduce the use of $this pseudo-variable in php?

任何时候我想使用实例变量,我都需要使用伪变量 $this,我说得对吗?如果这些是带有方法的对象,我仍然不得不使用它们。所以当我有这样的例子时:

class myClass{
    private $thingOne;
    private $thingTwo;

    public function __construct($thingOne,$thingTwo){
        $this->thingOne = $thingOne;
        $this->thingTwo = $thingTwo;
    }
}

然后我开始向 myClass 添加方法 我必须开始输入如下内容:

$this->thingOne->doSomething($this->thingTwo->doSomethingElse());

但我更愿意输入:

$thingOne->doSomething($thingTwo->doSomethingElse());

我只是觉得一直写这个-> 很烦人。难道不能假设我正在使用我给它的实例变量吗?我的代码最终到处都有这个这个这个这个这个这个这个。

我可以将它重命名为更短的名称吗? $t 什么的?

$this 是对当前 class 对象的引用,不能重命名。您可以创建自己的引用变量,但您必须在每个方法中声明它,从而降低效率。相反,您应该定期使用 $this.