Is there a way to avoid $this-> everywhere?

No, it's not possible to avoid that construct with PHP's built-in notion of OOP.

PHP, like JavaScript, Python and Perl -- but unlike Java and not always like Ruby -- always requires an explicit receiver -- or, $this for the "current instance" -- to access members. The syntax is just the form PHP happens to use to denote this construct and was likely heavily influenced by "being a late comer" to the language and having to fit it. It is also reminiscent of Perl/C syntax.

While the location can be altered or the number of sites may be reducible, at the end of the day, that is the method of accessing members.

Happy coding.


You could do this in each method

extract(get_object_vars($this));

Although that would only let you get variables, not methods, nor static properties. And they wouldn't be references, so only useful for reading.

Tags:

Php

Oop

Class