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 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