Learn by Doing: Practice PHP OOP (level 1)
Tags: php, laravel, beginners, tutorial
Published: 2021-04-06 09:45:13

---
title: Learn by Doing: Practice PHP OOP (level 1)
published: true
description: 
tags: php, laravel, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/iczmzu0j54ngchvwoeho.png
series: Python OOP
---

If you like this post and want more connect with me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

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?

<h1>Exercise #1</h1>
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

<h1>Exercise #2</h1>
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!

<h1>Solution:</h1>

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

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.

<h3>Exercise 1 solution:</h3>
```php
<?php

// Create a empty class
class Product
{

}

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

```

<h3>Exercise 2 solution:</h3>
```php
<?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;
```
<h1>Conclusion</h1>
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 
<a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8">