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

Hello! I'm Masui from the System Development Department!
This time, I would like to introduce the public part of public function and its friends.
Thank you for your attention!
First
First of all, the "public" in "public function" is called
an access modifier an access modifier allows properties (variables defined within a class) and methods (functions defined within a class) to be called from any class , or only from a specific class .
What is a class?
The word "class" appears frequently in this blog, but the class I am talking about here is an object-oriented class, which is a group of properties and methods .
Access Modifier Types
There are three types of
access modifiers public, private, and protected If you look at each one with its Japanese translation, the name and function match, making it easier to remember.
We will explain the three below.
public
// Public property definition public $variable // Public method definition public function method() { // Processing }
I'm sure there are some people who just add "punlic" to something without knowing what it means. (That's what I did a few years ago...)
In Japanese, "public" means something like "open to the public ," and properties and methods defined with "public" can be called from any class
Therefore, if you want to call something from another class, you define it as public.
Also, even if there is no access modifier, it will be treated as
a public method in PHP However, be careful because an error will occur if you do not add an access modifier to a property
// Public method definition function method() { // Processing }
protected
// Definition of a protected property protected $variable // Definition of a protected method protected function method() { // Processing }
The last word, protect, means to protect or prevent in Japanese, and in this case it protected , so it means something like being protected or prevented .
This protected property is related to a concept called object-oriented inheritance.
In object-oriented programming, the inheriting class is called the parent class, and the inherited class is called the child class, so I feel like it means protecting or guarding within a parent-child relationship. 🤔
Protected objects generally cannot be called from other classes . However, there is one exception They can only .
I think inheritance is used when you want multiple classes to have the same properties or methods, and things that you want to call only within that relationship are defined as protected
private
// Private property definition private $variable // Private method definition private function method() { // Processing }
In Japanese, the private means
something like private or non-public unlike public properties and methods defined with private, they cannot be called from other classes, but can only be called from the class in which they are defined .
This access modifier has no exceptions like the protected access modifier mentioned earlier (which can be called if inherited), and the property or method can only be called within the class in which it is defined.
Therefore, properties and methods that you only want to use in that class and don't want to be called from other classes are defined as private.
summary
The above is an explanation of what is called access modifiers.
To summarize, public can be called from anywhere, protected can only be called from classes in an inheritance relationship, and private can only be called from within the class in which it was defined.
Below is a table summarizing each access modifier.
| public | protected | private | |
| Can be called from the class where it is defined | ○ | ○ | ○ |
| Can be accessed from inherited child classes | ○ | ○ | ✕ |
| Can be accessed from an entirely different class | ○ | ✕ | ✕ |
When explaining protected, the concept of inheritance came up and it seemed a little difficult, but
I plan to write more about inheritance later, so please read on!
Thank you for reading this far🙇♂️
lastly
The system development service site I work for has launched "SEKARAKU Lab."
Beyond can handle everything from server design and construction to operation, so if you have any problems with server-side development, please feel free to contact us.
● SEKARAKU Lab: https://sekarakulab.beyondjapan.com
7