Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.