Access Modifiers in PHP. How to Use Getters and Setters

In a last tip, we learned how to create a class with properties, methods, and a constructor.

In this one, we are going to talk about access modifiers.

Access or visibility modifiers in PHP

Think of a class like a restaurant.

Customers in the dining room interact with the menu and waiters. These resemble public methods. Any customer can access them.

The kitchen and the cooks, on the other hand, resemble protected methods. You, as a customer, can be granted permission to enter the kitchen and talk to the cook, but only if she wishes so.

Finally, a proprietary restaurant’s recipe is like a private method. You don’t have access to it. Nor do the cooks or the staff in the kitchen have the right to grand you that access. The recipe belongs to the restaurant.

In PHP, the visibility modifiers are:

  • private: only the class itself can access it.
  • protected: the class and its child classes can access it.
  • public: any code can access it.

Let’s check them one by one.

The public modifier

Public is the default. For a property or method being public means that it can be accessed from anywhere.

Since public is the default behavior, you can leave off the modifier without any effect on visibility.

Code example of a class into which we leave off the public modifier.

The private modifier

Then, we have private. This modifier indicates that the property or method can only be accessed from within the class.

But there are cases where you do want to access a private property or method from outside the class. And there is a way to achieve this by using getters and setters.

Getters and setters are special methods that you can use to modify (setter) or access (getter) private properties and methods.

Let’s see a simple example:

Code example of a class with a private property.

Here we have the status private property. If you want to access it directly, you can’t. You can only access it inside the class.

But you can create a public function. That function does have access to this private property since they both live in the same class.

Now, this public function, I can use it in the global scope. So I can have access to the private status property INDIRECTLY through a public function.

We call these functions getters.

Let’s see how to use one.

Code example of a class using a getter.

There are also cases where we want not only to get access to a private property but also to be able to modify it.

We can do this by using a setter.

A setter is a public function that enables us to modify a private property or method and it is built in the same class.

This function has a parameter that helps set a new value. The argument we pass in updates (sets) the private property.

Code example of a class using a setter.

Now when we read the private property (through the getter), we see that it is changed (through the setter).

The protected modifier

When a property or method is marked as protected, it means that it can be accessed from within the class it belongs to or any class that inherits from it.

If a property or method is defined in a parent class, you can access it in any subclass that inherits from that parent class. However, you cannot use that property or method in a different class that does not inherit from the parent class, nor can you access it in the global scope.

A protected property behaves the same as a private one, in the sense that you can read it through a getter and modify it through a setter.

But a protected property/method differs from a private one in that you can have access to it through inheritance.

So classes that inherit from a parent class have access to that parent class’s protected properties or methods.

Choosing the Right Visibility Modifier in PHP

Choose a class member’s visibility based on who genuinely needs access .

A good practice is to implement a restrictive strategy. According to this, if a property is only for internal use, you should keep it private. This gives you the freedom to change how the class work without breaking other code.

The idea is that it is easier to make a property public if it previously was restricted, than vice versa.

In the former case, you just allowing more access. In the later, things are more complicated.

Moving from public to private, or protected for that mater, can lead to code breaking.

Over the months, other files, plugins, or colleagues might reference that public method. Now you have to hunt down all these cases.

Start restrictive

If a property is only an internal implementation detail, keep it private. That gives you freedom to change how the class works without breaking other code.

Code example of a class with a private property.

Here, $balance is private because outside code should not be able to set it freely:

Code example of a public and a private method.

If $balance were public, any caller could put the account into an invalid state:

Code example of a class method with an access modifier.

Wrapping Up

In PHP, access modifiers are keywords that control the visibility of class properties and methods. The three main access modifiers are public, which allows access from global scope; protected, which restricts access to the class itself and its sub-classes through inheritance; and private, which limits access only to the same class.

When define the visibility of your methods and properties, consider for each one if outside code needs it.

If it doesn’t then make it private. If it is only needed by another subclass, or you write code for inheritance, then make it protected.

Only add public access to the methods that are meant to be part of your class’s interface. Those that other objects and client code need to call to interact with it. Everything else should remain hidden as implementation details,

Although my blog doesn’t support comments, feel free to reply via email or X.