Join Kevin Skoglund for an in-depth discussion in this video Challenge: Static references, part of PHP: Object-Oriented Programming.
- [Instructor] It's time for another challenge assignment. This challenge relates to everything we've done in this chapter on static properties and methods. In order to do this challenge, we're going to be reusing the Bicycle and the Unicycle classes that we've created in the last challenge, so the very first thing you want to do is create a new file called challenge_04.php, then you'll go and open up challenge_03.php and copy those classes for Bicycle and Unicycle into the new file, then you'll be ready for the challenge.
There are a couple of different parts to this challenge. The first is that I want you to add a static property called $instance_count. The idea is that it'll be used to store a count of the number of instances that have been created. Once we have that static property, we can create a static method called create(). The idea behind create() is that create() is going to create and return a new instance to us, so it'll do two things. It's going to add one to that $instance_count variable and it's going to create and return a new instance of the class.
Now, make sure it's the correct class. If it's called on Bicycle, it should create and return a Bicycle instance. If create() is called on Unicycle, it should create a Unicycle instance. It's a little bit tricky, so spend some time thinking about that. And then, of course, it's a best practice to make sure we use visibility modifiers on both $instance_count and create(). The second part of the challenge relates to constants. I want you to define a constant for storing an array of categories. The idea is that these are categories that a bicycle could belong to.
So, for example, Road, Mountain, Hybrid, Cruiser, City, and BMX. If you're using PHP 7.1 or later, you'll also want to set a visibility monitor on that constant and you can set it to public so that we can see it when we're working with a instance. If you're using PHP before 7.1, you don't set the modifier, it'll automatically be public. Then, add a $category property for instances so they can store their category. That way, one instance of a bicycle can be a mountain bike, while another instance of a bicycle can be a hybrid bike.
For the third part of the challenge, I want you to make the property $wheels into a static property. We've already defined it, but now it needs to become a static property and also make wheel_details() into a static method. Once you've done that, then make sure that both Bicycle and Unicycle return the expected results for wheel_details(). Unicycle's the one to really watch out for, that's a little tricky because it's a subclass. And then, last of all, I have an extra credit assignment for you. If you feel like you have a good handle on this, try to create a method in Unicycle which extends a method in Bicycle by executing code either before or after the Bicycle's method, and then, create another method in Unicycle which overrides a method in Bicycle, but will fall back to the original method if a condition is not met.
What the behavior is in the parent or the subclass is really up to you, get creative. The point is to try and implement these two design patterns. If you need help, you can go back to the movie on referring to the parent class to get some ideas. Spend a few minutes working on this challenge and make sure that you understand static properties and static methods. In the next movie, I'll show you the solution that I came up with.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project