[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[For beginners] About PHP constructors

Hello!
My name is Matsuki from the System Development Department.
This time he will take a deep look at one of the features of object-oriented programming languages: constructors.

What is a constructor?

A constructor is a method that is always executed when a class is instantiated (objectified). Normal methods can be called at any time after instantiating a class using the arrow operator, but constructors are automatically called at the time of instantiation, so there is no calling process.

・Normal method

$object = new class name; $object->method name();

・Constructor method

$object = new class name;

How to write a constructor

The constructor method is written as follows.

public function __construct() { // Processing content }

The first word "public" is written, but this is one of the access modifiers.

Access modifiers include public, protected, and private, but if you do not set it to public, the constructor method will not be called when the class is called from outside, so basically use the public access modifier.

Also, a method with two underscores and "__" in front of the method name is called a magic method. Magic methods are methods that are called in special situations, and in addition to constructors, there are also ``__get'' and ``__set.'' I digress, but constructors can be written and used as shown above.

Usage example

From here, let's actually use it and establish the image. First, create a class. This time, we will create a human class, the 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 "name, age, and birthplace" as necessary properties (variables). When calling this class, the number specified in the constructor argument must be included in the argument when creating the instance.

$human = new Human('taro', 32);

By writing the code above, we were able to instantiate the Human class by passing the necessary values ​​to the constructor. $human stores the Human class.

A question arises here.

"There are three constructor arguments, but when instantiating only two are specified, why can I create one?"

That's the question.

If you look closely at the contents of the constructor, you will see that the third argument $from has the value Japan. In this way, in constructors and PHP methods in general, you can set an initial value such as entering this value if nothing is received. If a value is received, the received value is assigned and the initial value is not called.

Next why variables are designated as private , it is because it is prohibited to change them directly from the outside. This type of writing is called encapsulation

So, "What's wrong with making changes directly from the outside?"

For example, if you only want to enter values ​​up to the age of 200 in the age variable $age, and you enable direct changes, you will be able to enter values ​​after the age of 201. Therefore, we use a method that encapsulates the value, passes it through a method, etc., checks it with conditions to determine if it is an invalid value, and then assigns it to a variable.

In this case, we have prepared a set method and written the judgment expression within the set method.
By calling that set method in the constructor, when the Human class is instantiated, setAge() is called to limit the value that goes into $age.

In the example below, when a value of 201 or higher is received, 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 constructors

By initializing variables in the constructor, the variables of the newly created instance will always contain the specified values. This feature allows variables to have values ​​for each object.

summary

What did you think?

This time I focused on constructors, but there are many other things to learn about object orientation, so let's keep learning!

That's all for this blog.
Thank you for reading to the end.

If you found this article helpful , please give it a like!
17
Loading...
17 votes, average: 1.00 / 117
9,804
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Matsuki

I like poker and rugby.
MARVEL likes “Hulk”

My motto is concentration x time + luck