I’m a Java developer who has turned to the dark side over the last couple of weeks as I’ve been trying to write some code in PHP for the first time ever.
Most stuff seems familiar, methods become functions, . becomes ->, variables have a $ in front and you never have any idea what type they are, I get it, but I’m totally stumped by the $this keyword.
I expect $this to call a function in the current class as in java this.myCoolMethod(), but often as I poke through the open source project I’m going through, $this->myCoolMethod() calls a function in a totally different class!
What is happening here? What does ‘$this’ actually mean?
Thank you for any help.
John
The reason it sometimes calls a method in a totally different class, is because that class probably extended another class with that method.
Here is a quick example:
As you can see, even though whatwhat() isn’t in the class, it inherits it from the class it extends.
Hope that helps.