doulai6469 2016-04-22 19:00
浏览 44
已采纳

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

Any time I want to use an instance variable, I need to use the pseudovariable $this, am I correct? And if those are objects with methods, I'm still having to use them. So when I have an example like so:

class myClass{
    private $thingOne;
    private $thingTwo;

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

And then I start adding methods to myClass I have to start typing things like:

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

But I would much rather type:

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

I just find writing this-> all the time to be annoying. Can't it just be assumed that I'm using the instance variables that I have given it? My code ends up having this this this this this this this everywhere.

Could I rename it to something shorter? $t or something?

  • 写回答

1条回答 默认 最新

  • dongtang3155 2016-04-22 19:28
    关注

    $this is a reference to the current class object and cannot be renamed. You could make your own reference variable, but you would have to declare it in every method, thus making it inefficient. Instead you should just regularly use $this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算