DEV Community

loading...
Cover image for Learn by Doing: Practice PHP OOP (level 3)

Learn by Doing: Practice PHP OOP (level 3)

ericchapman profile image Eric Chapman ・2 min read

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

In exercise #4 we created a class with a constructor using property promotion:

<?php
class Product 
{
    public function __construct(private $name, private $description, private $price) 
    {
    }
}
Enter fullscreen mode Exit fullscreen mode

Today your challenge is to create a getter and setter to modify the name property.

Hint: Getter and Setter are 2 methods:

The getter method is a method that get (return) the object instance property value.

The setter method is setting the current instance property value to a new supply value.

Let's do it!

Exercise #6

A quick one: When setting the property name. Make sure that the name first letter is Uppercase.

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

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

    }

    public function setName($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Exercise 6 solution:

public function setName($name){
    $this->name = ucfirst($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

Discussion (2)

pic
Editor guide
Collapse
johnbetong profile image
John Betong

Why not use the following PHP features?

  1. strict_types // file wide and ensures type is correct otherwise throws errors
  2. set error_reporting(-1); // applies to all include/required files - max error reporting
  3. ini_set('display_errors', 'TRUE'); applies to all include/required files - show errors on screen
  4. declare function parameter types
  5. declare function parameter result type

If the above is not used then you might as well go back to using PHP 4 :)

Also enhances the chances of using JIT Compilation which will no doubt not tolerate "lazy type juggling"

Collapse
ashishakya profile image
Ashish Shakya

why did you stop posting?? where are the other levels???