DEV Community

Cover image for Learn by Doing: Practice PHP OOP (level 1)
Eric The Coder
Eric The Coder

Posted on • Edited on

Learn by Doing: Practice PHP OOP (level 1)

If you like this post and want more connect with me on Twitter: Follow @justericchapman

You want to learn PHP OOP for a long time but you keep postponing? Now is the time! For the next 30 days I will post php OOP exercises + solution.

Your challenge is to try to solve the small exercise without looking at the solution. You can use the web to search for concept but please dont look at the solution before at least try to solve the exercise.

We will start very easy but don't be bored because exercises will quickly became more and more challenging....

Challenge accepted?

Exercise #1

In OOP class is like the blueprint and object are the instance. For example a house plan would be the class but the houses that will be construct base on that plan will be the objects.

Your first challenge is to create a class name Product and create a instance of that class name product1

Exercise #2

Since exercise #1 its pretty easy why not go for another one right now.

So we have a class named Product. A Product can have properties like name, description, price, etc. We can define variables like $name, $description, and $price to hold the values of these properties.

When the individual objects (product1, product2, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

For the second exercise add properties (name, description and price) to the class Product. A little hint. We create properties like we create a variable but the property need to have an access modifier prefix: public, private or protected. We will cover more on that later. For now use the public prefix.

After that go to the instantiated object product1 and modify the property name for 'iPhone 12'. Then you can: echo product1->name to see the result.

Last but not least, create a second object name product2 and set is name property to 'iPhone 12 Pro' and echo out that property.

Ready? Let's do it now!

Solution:

STOP... Do the exercises first! It's the only way you will really learn.

If you have done your exercise here my solution. Noted most of the times, more than one solution will be possible. If you have something different that me, leave your solution in the comment to share and discuss.

Exercise 1 solution:

<?php

// Create a empty class
class Product
{

}

// Instantiate the class into a object
$product1 = new Product;

Enter fullscreen mode Exit fullscreen mode

Exercise 2 solution:

<?php
class Product
{
    // properties
    public $name;
    public $description;
    public $price;
}

// Create new instance
$product1 = new Product;
$product2 = new Product;

// Set instance property name
$product1->name = 'iPhone 12';
$product2->name = 'iPhone 12 Pro';

// Display on screen
echo $product1->name;
echo $product2->name;
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today. Tomorrow the journey continue, see you later!

If you want to be contribute you can re-tweet this post on tweeter
Follow @justericchapman

Top comments (0)