I’m confused about these two keywords and the way to use them in PHP5. I think that “this” is used for instanced objects (not static) while “self” is referring to the object itself, not an instance of it and thus used within static objects. Right?
Now, I believe that the correct use inside a class’s static method to call another static variable/method is the following:
self::doSomething();
self::$testVar;
That’s true?
However, the following also seems to be possible:
$self->testVar;
Yet, $testVar is static. Why is that?
Also, why is $ used infront of self sometimes and sometimes not, and same question for “this” keyword?
You’re right, self is for static self-references while $this is for instantiated ones. self and $this might appear to work everywhere but consider this:
This results in a fatal error because foo() was called statically. It’s best to take some time and use them appropriately rather than always using one or the other.