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

Illustration of a person holding a shield

Hello! I'm Masui from the System Development Department!

This time, I'd like to introduce the `public` keyword in `public function` and its related functions.
Thank you for your attention!

First

First, the "public" in "public function"an access modifieris called
Access modifiers determinea property(a variable defined within a class) ora method(a function defined within a class) can be calledfrom any classwhetheronly from a specific class.

What is a class?...
The word "class" appears frequently in this blog, but the class I'm referring to here is an object-oriented class, which is acollection of properties and methods grouped together.

Access Modifier Types

access modifiers public, private, and protected There are three types of
Knowing their Japanese translations might make them easier to remember, as the names and functions will match.
The following explains these three.

public

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

Some people might just add `public` to their properties without really knowing why. (I was like that a few years ago...)
` public` in Japanese, means something like"open" or "publicly accessible," and properties and methods defined as `public`called from any classcan be

Therefore,anything that I want to call from another classis defined as public.

Furthermore,without access modifiersmethods public methods in PHPare treated as
However,if you don't specify an access modifier for a propertyit's important to note that an error will occur

// 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 something like "to protect" or "to prevent" in Japanese, and in this case, " protected" .means something like "protected" or "prevented"

This `protected` keyword is related to a concept called inheritance in object-oriented programming.
In object-oriented programming, the class that inherits is called the parent class, and the class that inherits is called the child class. It seems like `protected` means protecting or safeguarding something within that parent-child relationship. 🤔

Things defined with `protected`generally cannot be called from other classes. However,one exceptionthere isOnly when inheritance occursそのcan a method be called within that relationship.

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 }

The private" in Japanesemeans something like "private" or "confidential.
" Unlike "public," properties and methods defined as "private" cannot be called from other classes, and can only be called from the class that contains them.

This access modifier, unlike the previously mentioned `protected`,does not have any exceptions (it can be called if the class inherits it), and it completely restricts the access of properties and methods to only those within the class in which it is defined.

Therefore,properties and methods that you only want to use within that classand that you don't want to be called from other classes are often defined as private.

summary

The above explains what are called access modifiers.
To summarize, `public` means that a method can be called from anywhere, `protected` means that it can only be called from classes that inherit from it, and `private` means that it can only be called from within the class that defines it.
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

The explanation of `protected` in the middle of the explanation introduced the concept of inheritance, which might have made things a little more complicated, but
I plan to write about inheritance later, so
I hope you'll read that as well!
Thank you for reading this far 🙇‍♂️

lastly

I've launched "SEKARAKU Lab," a service site for the system development company I belong to.
Beyond offers a one-stop service from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.

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

If you found this article helpful,please give it a "Like"!
8
Loading...
8 votes, average: 1.00 / 18
8,864
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

anaesthesia

I joined the company as a new graduate in April 2022 and
am currently in the Systems Development Department.
Nice to meet you!