dtech
dtech

Reputation: 49289

"this" mandatory for member access from inside the class body in PHP?

Coming from C++ I am used to be able to access class members directly in the body of their class, however, this doesn't seem to work in php - simple setters and getters fail to work unless explicitly using $this-> to access them. Setters seem to set to a temporary object that gets discarded and getters generate an error of trying to access non-existing objects.

Is there a way to directly access members inside the class body without the this keyword in php?

Upvotes: 1

Views: 366

Answers (2)

troelskn
troelskn

Reputation: 117467

No, there is not. Setting an undefined variable will create it for the current scope, so that's what you're observing. (This is not a member variable though) - You can even read from an undefined variable, in which case the value will be null. This will generate an E_NOTICE though, so it's not considered good style.

Upvotes: 3

ka_lin
ka_lin

Reputation: 9432

Please read http://php.net/manual/en/language.oop5.php give a code snippet to see the exact problem you are facing

Upvotes: 0

Related Questions