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

The use of static properties in real world

What is the use of Static methods and properties in real world?

1 Answer

Shafeeq, as you know, static properties are class properties as opposed to instance or member properties -- they don't change from object to object. So what would make sense to have as class properties.

Say you have an Employee class and you want to assign a unique ID to each employee. You would have an employeeID instance (non-static) property. But you would also have a lastID property to store the number assigned to the last employee created, and you would increment it by 1 for the next employee in your constructor. You wouldn't want lastID to be non-static, and vary from object to object. You want it to be the same for all objects. It couldn't be a constant, because it needs to change every time a new employee is created. But when it changes it changes the same for each object.

Hope this helps.

Thanks Jcorum