[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”

What does public mean in public function? ? [Access modifier]

Illustration of a person with a shield

Hello! This is Masai from the System Development Department!

This time, I would like to introduce public of public function and its friends.
Thank you very much!

first

First of all, public in public function is called
an access modifier an access modifier is a property (variable defined within a class) or method (function defined within a class) that can be accessed from any class. It allows you to call it it allows you to call it only from a specific class

What is a class?
The word class comes up often in this blog, but the class I am writing here is an object-oriented class, and refers to a group of properties and methods. .

Access modifier type

There are three types of
access modifiers: public, private, and protected If you try pairing each with a Japanese translation, the names and functions will match and it may be easier to remember.
I will explain three of them below.

public

// Public property definition public $variable // Public method definition public function method() { // Processing }

I'm sure there are some people who don't know what it is, but just keep punlic on. (This happened to me a few years ago...)
In Japanese, public has the meaning of public , and properties and methods defined with public can be called from any class. It will be.

Therefore, anything that you want to call from another class is defined as public.

Also, even if there is no access modifier treats it as
a public method However, be careful as an error will occur if you do not add an access modifier to the property

// Public method definition function method() { // Processing }

protected

// Definition of protected property protected $variable // Definition of protected method protected function method() { // Processing }

The last word "protect" in Japanese means to protect or prevent, and in this case, it means "protected" or " prevented " .

This protection involves a concept called object-oriented inheritance.
In object-oriented technology, the class that inherits is called the parent class, and the class that inherits from is called the child class, and I feel that it means something like protecting or protecting within a parent-child relationship 🤔

What is defined with protected is basically Generally speaking, it cannot be called from another class . However, there is one exception That is inheritance, and only can it be called within that relationship .

Inheritance is used when you want multiple classes to have the same properties and methods, and you use protected to define things that you want to call only within that relationship.

private

// Definition of private property private $variable // Definition of private method private function method() { // Processing }

In Japanese, " private means
"private" or "private" and unlike "public", properties and methods defined as "private" cannot be called from another class; they can only be called from the class that has the one defined as "private". .

This access modifier does not have any exceptions like the previously mentioned protected (which can be called if it is inherited), and it becomes a property or method that can only be called by the class that defines it.

Therefore, properties and methods that you want to use only in that class and that you do not want to be called from other classes are defined as private.

summary

The above is an explanation of what is called an access modifier.
In summary, public can be called from anywhere, protected can only be called from classes that have an inheritance relationship, and private can only be called from within the class that defined it.
Below is a table summarizing each access modifier.

public protected private
Can be called from the defined class
Can be accessed from inherited child classes
Can be accessed from a completely different class

During the explanation of protected, the concept of inheritance came up and it seemed a bit difficult, but
I plan to write about inheritance later, so please read it when that happens!
Thank you for reading this far 🙇‍♂️

lastly

I have opened SEKARAKU Lab, a system development service site to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.

● SEKARAKU Lab: https://sekarakulab.beyondjapan.com

If you found this article helpful , please give it a like!
7
Loading...
7 votes, average: 1.00 / 17
6,415
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

anaesthesia

I joined the company as a new graduate in April 2022 and
currently belong to the system development department.Thank
you for your continued support!