Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Andrew Hawes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andrew Hawes
Python Web Development Techdegree Graduate 28,105 Points

Referencing PHP class variables without $this?

I was wondering if anyone had a practical way of referencing PHP class variables inside class methods without using $this. For now, (when I only need to read variables) I've just been either redeclaring the variable inside a method ($variable = $this->variable) or passing it through as an argument.

Is there some kind of way I can at least declare the variables' scope in a similar way that one does when using global variables? (global $var1, $var2, etc.)

2 Answers

Petros Sordinas
Petros Sordinas
16,181 Points

Andrew,

Php class properties can only be referenced by $this->propertyName or static::propertyName (if declared as static).

Other than writing getter and setter methods, I can't think of another way to reference your properties.

Why are you troubled by this anyway?

Andrew Hawes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andrew Hawes
Python Web Development Techdegree Graduate 28,105 Points

Thanks for the reply. That's what I was afraid of. I suppose I don't have any practical reason for it to bother me other than that I dislike having to type $this-> before everything. It should be unnecessary and just makes for more to read and write.