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 Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Static Methods and Properties

Gregg Squire
Gregg Squire
14,829 Points

If you are using a static property, why would you create a shirt object and then "echo $shirt::$manufacturer;"?

Isn't the whole point of using a static property or method so that you don't need to create an object, so you can type "echo product::$manufacturer;"?

Jeff Styles
Jeff Styles
2,784 Points

I agree. This has just frustrated me greatly. Every example shown on accessing the value of the $manufacturer property was done via an object. Then, the very first quiz question asked how to access getMaker() from OUTSIDE of the class - Product::getMaker();

4 Answers

Andrew Shook
Andrew Shook
31,709 Points

No, the point of a static property or method is to have a variable or function that is available to all objects of that class.

If function is public it is already available to all objects of that class. From what I understand the point of a static property or method is to have a variable or function from the NOT instantiated class available in the global scope to all objects and all classes. Not instantiated class means a class does not have an object yet, therefore it sits there idle, waiting to be activated into the memory. Using static we can make those variables or methods in the idle class available in the global scope. Thus the questions Gregg Squire asks remains valid.

Tom Hudock
Tom Hudock
855 Points

I agree with Gregg and Andrew. This part of the tutorial doesn't make sense. If the whole idea of using :: is so that you can access properties and methods in an object without having to instantiate them, then why create the $shirt object and then call $shirt::manufacturer? Wouldn't Product::manufacturer be the proper way to access a static property in a class that hasn't been instantiated?

Yep, the course examples are confusing. I understand the concepts but the examples don't really show why you would use the static keyword. It reminds me of situations when im in a conference room discussing issues with my fellow developers, then the next sentence they say "OK let me explain it another way" LOL. I still give props to the teacher. Its just hard sometimes explaining certain concepts.

This tutorial is bit confusing! This tutorial doesn't explain static property and method right way, examples in this tutorials are using objects to access static methods or properties but defination on http://us2.php.net/manual/en/language.oop5.inheritance.php says something like,

"Declaring class properties or methods as static makes them accessible without needing an instantiation of the class."