top of page

Posts

What is a Public Function? (Access Modifier)



Introduction

First of all, the “public” of the public function is called an access modifier, access modifiers allow properties (variables that are defined within a class) and methods (functions that are defined within a class) to be called from any class or only from a specific class when they are used to access.


What is lass...?

In this article, you’re going to see the word “class”. the class here means an object-oriented class, which is a collection of properties and methods.


Type of access modifiers

There are three types of access modifiers which are “public” “private” and “protected”.

public

// Public Property Definitions
public $variable
 
// Public Method Definitions
public function method()
{
    // Process
}

There are probably some people who have no idea that they just put public for the time being. (That was me a few years ago…)


The property and method that are defined in public, just like the meaning of public, can be called from any class.


Therefore, when you want to call them from another class, it needs to be defined as public.


Also, a method without an access modifier is treated as a public method in PHP.

However, if you create a property without an access modifier it will result in an error.


// Public Method Definitions
function method()
{
    // Process

protected

// Protected Property Definition
protected $variable
 
// Protected Method Definition
protected function method()
{
    // Process
}

Protected definitions cannot basically be called from another class. However, there is one exception. That is inheritance, which can only be called in that relationship when inheritance is performed.


Inheritance is used when you want to have the same property or method in multiple classes, and you want to call it only within that relationship, so you define it as protected.

private

// Private Property Definition
private $variable
 
// Private Method Definition
private function method()
{
    // Process
}

This access modifier is a property or method that can be called only in the class in which it is defined, with no exception (it can be called if inherited) like protected.

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 can be defined as private.


Summary

That’s all the explanation of access modifiers.

In summary, public can be called from anywhere, protected can be called only from classes in an inheritance relationship, and private can be called only from within the defined class.


Below is a table summarizing each of these access modifiers.



public

protected

private

Can be called from defined class

Accessible from child classes that have inherited it

Accessible from a completely different class



The concept of inheritance might confuse you but I’d love to talk about inheritance in another article.

Thank you!


This blog post is translated from a blog post written by Masui on our Japanese website Beyond Co..

Comments


bottom of page