[For beginners] About PHP constructors

Hello!
I'm Matsuki from the System Development Department.
Today, I'd like to take a closer look at constructors, one of the features of object-oriented programming languages.
What is a constructor?
A constructor is a method that is always executed when a class is instantiated (made into an object). Normal methods can be called at any time after a class is instantiated using the arrow operator, but a constructor is called automatically when the class is instantiated, so there is no need to call it
・Normal method
$object = new classname; $object->methodname();
・Constructor method
$object = new className;
How to write a constructor
The constructor method is written as follows:
public function __construct() { // Processing content }
The word public is written at the beginning, which is an access modifier
There are three access modifiers: public, protected, and private. If you do not use public, the constructor method will not be called when the class is called from outside, so you should generally use the public access modifier
Additionally, methods with two underscores "__" before the method name are called magic methods. Magic methods are methods that are called in special situations, and in addition to constructors, there are also "__get" and "__set". I've digressed, but constructors can be written and used as shown above
Usage example
Now let's actually use it and get a feel for it. First, create a class. In this example, we'll create a Human class
class Human { private $name; private $age; private $from; public function __construct( $name, $age, $from = 'Japan' ) { $this->name = $name; $this->age = $age; $this->from = $from; } }
Since we are creating a human class, we have prepared "Name, Age, Place of Origin" as the necessary properties (variables). When calling this class, the number specified in the constructor argument must be entered as an argument when creating an instance
$human = new Human('taro', 32);
By writing it as above, we were able to pass the necessary values to the constructor and instantiate the Human class. $human contains the Human class
Here, a question arises
"Why is it possible to create an object even though the constructor takes three arguments and only two are specified when instantiating it?"
This is my question
If you look closely at the contents of the constructor, you'll see that the value Japan is assigned to the third argument, $from. In this way, constructors and PHP methods in general allow you to set an initial value to be entered if nothing is received. If a value is received, the received value is assigned and the initial value is not called
Next , why private variables are specified ? This is because they are prohibited from being changed directly from the outside. This style of writing is called encapsulation
So, what's wrong with making changes directly from the outside?
For example, if you want to set only values up to 200 in the age variable $age, but you allow direct changes, values from 201 onwards can also be set. For this reason, we use encapsulation and always use methods etc. to check whether the value is invalid before assigning it to the variable
In this case, we have prepared a set method and written the judgment expression within the set method.
By calling this set method in the constructor, when the Human class is instantiated, setAge() is called and the value entered in $age is restricted.
In the example below, if a value of 201 or higher is received, the value 200 is forcibly entered
class Human { private $name; private $age; private $from; public function __construct( $name, $age, $from = 'Japan' ) { $this->name = $name; $this->setAge($age); $this->from = $from; } public function setAge($age) { if ($age <= 200) { $this->age = $age; } else { $this->age = 200; } }
・Advantages of using a constructor
By initializing variables in the constructor, the variables of the newly created instance always have the specified values. This function allows variables to have values for each object
summary
What did you think?
This time we focused on constructors, but there is a lot more to learn about object-oriented programming, so let's continue studying!
That's all for today's blog.
Thank you for reading to the end.
20