What does public mean in public function? ? [Access modifier]
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