What is the utility of requiring the $this-> before calling a method from within its own (or a child’s) class? What is this meant to prevent? Wouldn’t it be logical to think that the engine should look within the class by default?
What is the utility of requiring the $this-> before calling a method from within
Share
Consider the following example:
So, without
$this->prefixing the constructor’s call tobar(), PHP calls the outside function and not the method. Using$this->eliminates the ambiguity and precisely defines which function you want to call.If what you have suggested were true (auto searching the class), then as far as I know (waiting for crazy unreadable hack in comments), it would be impossible in PHP for a class method to call a function in the global scope that has the same name as another class method.