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 …


This content originally appeared on DEV Community and was authored by Eric Chapman

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);

Exercise 4 solution:

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

    }
}

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


This content originally appeared on DEV Community and was authored by Eric Chapman


Print Share Comment Cite Upload Translate Updates
APA

Eric Chapman | Sciencx (2021-04-07T17:45:36+00:00) Learn by Doing: Practice PHP OOP (level 2). Retrieved from https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/

MLA
" » Learn by Doing: Practice PHP OOP (level 2)." Eric Chapman | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/
HARVARD
Eric Chapman | Sciencx Wednesday April 7, 2021 » Learn by Doing: Practice PHP OOP (level 2)., viewed ,<https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/>
VANCOUVER
Eric Chapman | Sciencx - » Learn by Doing: Practice PHP OOP (level 2). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/
CHICAGO
" » Learn by Doing: Practice PHP OOP (level 2)." Eric Chapman | Sciencx - Accessed . https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/
IEEE
" » Learn by Doing: Practice PHP OOP (level 2)." Eric Chapman | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/. [Accessed: ]
rf:citation
» Learn by Doing: Practice PHP OOP (level 2) | Eric Chapman | Sciencx | https://www.scien.cx/2021/04/07/learn-by-doing-practice-php-oop-level-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.