DEV Community

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

Posted on • Updated on

Learn by Doing: Practice PHP OOP (level 2)

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 #3

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!

Exercise #4

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.

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 3 solution:

<?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);
Enter fullscreen mode Exit fullscreen mode

Exercise 4 solution:

<?php
class Product 
{
    public function __construct(private $name, private $description, private $price) 
    {

    }
}
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 (5)

Collapse
 
kralik12 profile image
Michal Král

Hi, is there any reason for not using typed properties and parameters in your examples?

Collapse
 
ericchapman profile image
Eric The Coder

Hi, yes the reason is to introduce the concept one piece at time. The typed property will come soon in a futur challenge

Collapse
 
kralik12 profile image
Michal Král

Thanks for your answer. IMO this is wrong approach - typed properties/arguments are core feature of the PHP now and when you are trying to teach someone to code you should teach them all the features. If nothing else, static types prevent a lot of bugs

Thread Thread
 
catwhocode profile image
Cat who code

Michal, he said "will come soon", isn't that clear ?

Collapse
 
ozh profile image
྅༻ Ǭɀħ ༄༆ཉ

TIL about PHP 8's Constructor Property Promotion. Neat :) Thanks!