Why is the usage of $this in PHP necessary when referencing methods or variables in the same class?

后端 未结 5 1981
你的背包
你的背包 2021-01-16 04:24

I was explaining to a Java developer why his method call wasn\'t working. He just needed to add $this->method_name();

He then asked me, \"Why do

5条回答
  •  梦毁少年i
    2021-01-16 04:35

    If I have to guess: Because it was easier than the alternatives. Object oriented support in PHP has always been very much of a hack. I vaguely remember reading a discussion about the upcoming closure support that will appear in PHP 5.3. Appearently it was really, really hard to implement lexical closures in PHP due to it's scoping rules. Probably because you can nest a class in a function in another class and stuff like that. All that freedom possibly makes stuff like this incredibly hard.

提交回复
热议问题