Learn by Doing: Practice PHP OOP (level 2)
Tags: php, laravel, beginners, tutorial
Published: 2021-04-07 17:45:36

---
title: Learn by Doing: Practice PHP OOP (level 2)
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 #3</h1>
For this exercise, add an object initialization method inside of the Product class. (hint: function __construct())

With that construct function get as parameter the name, description and price and assign them to instance attributes of the same name (hint: The current instance is reference by '$this' keyword)

After that create two object (product1 and product2). When you will initialize the object ex. $product1 = new Product('iPhone 12', 'This is a great iPhone', 799.99) you pass the three parameters (name, description and price)

Ready? Let's do it now!

<h1>Exercise #4</h1>
In the new PHP 8 version. Class constructor have a new shorthand way for declaring properties. That feature is call 'Constructor Property Promotion'

Your challenge is to convert the class and property created in the last exercise and refactor it with the 'constructor property promotion' shorthand syntax. You can search online if you have never heard of that new syntax.


<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 3 solution:</h3>
```php
<?php

class Product
{
    // properties
    private $name;
    private $description;
    private $price;

   function __construct($name, $description, $price) {
       $this->name = $name;
       $this->description = $description;
       $this->price = $price;
   }
}

$product1 = new Product('iPhone 12', 'This is a great iPhone', 799.99);
$product2 = new Product('iPhone 12 Pro', 'This is a great Pro iPhone', 999.99);
```
<h3>Exercise 4 solution:</h3>
```php
<?php
class Product 
{
    public function __construct(private $name, private $description, private $price) 
    {

    }
}
```

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