How to Think like a Programmer | PHP OOP Project | Create objects with PHP

preview_player
Показать описание
The ability to separate something into objects is one of the most important skills when you create programs. In this tutorial we are going to create a Car and add some separated components.

1. Build Car class.
The Car class has the following properties: Color - which is fundamental for any object and Lights. The constructor of the require all fundamental parts and properties of the car. In this case the Lights are not fundamental, so we can add them on later phase to our objects. For now we allow adding of lights trough a method. We also allow changing of the color.

2. Car Components - Light class.
As any object in the real world, the Car has many related components. In OOP separating the components into classes is a good practice. Lets crete the Light class.

3. Front left light and Front right light.
The Car has many light - on the front, on the back, on the left and right side sometimes. We are creating a class that hold all kind of light in the car.
In this case front lights. The both lights are required in class constructor because they are fundamental.

4. Add lights collection to the Car object.
Now we have the lights collection and we can add them to the car. We gone use BMW object created earlier. Lets turn the left light ON and see what's happen.

5. Create Road class for multiple car objects.
When you have particular class, you are able to create as much objects as you want. In this case we can create many car objects and attach them to the Road class as it in the real world - one Road has many cars.
Рекомендации по теме
Комментарии
Автор

That's a great explanation, thank you for that! Keep making more videos like this :)

mariadimitrova
welcome to shbcf.ru