In the previous post, we introduced Object Oriented Programming in PHP. We discussed the difference between Class and Object. Also, we learned to create a basic Class in PHP. In this article, we will discuss Access Modifiers.
If you are new to Object Oriented Programming, I would recommend you to go through my first article here.
What are Access Modifiers?
Access Modifiers are used to set visibility access rights of the PHP methods and member variables in a Class.
The Visibility of a class property (a method or variable) and as of PHP 7.1, a constant can be defined by the use of three access modifiers namely public, private and protected.
Public: A public member is accessible from even outside the class. Anyone can read or modify the value of a public member.
Private: A private member is only accessible by the class itself. It cannot be accessed outside the class.
Protected: A protected member is accessible only by the class or the child classes. Protected Access Modifiers are discussed on a separate post.
Public vs Private
For example, to demonstrate public and private members, consider the following the scenario:
<?php class Car { public $name; private $modelYear; } $bmw = new Car(); $bmw->name = 'BMW Series 3'; echo $bmw->name; //BMW Series 3 ?>
In the class Car, we have two member variables now.
Now we can directly set the name of the car with object reference outside the class as shown in the example. However, if we try to set the private modelYear directly, we will get the following error:
$bmw->modelYear = '2019';
//Fatal error: Uncaught Error: Cannot access private property Car::$modelYear in /var/www/html/test/class.A.php
A Fatal error is thrown while trying to access private property. Private properties can only be accessed within the class. Hence, if we would like to set a modelYear in this case, we can use a public method as shown below.
<?php class Car { public $name; private $modelYear; public function setModelYear($modelYear) { $this->modelYear = $modelYear; } } $bmw = new Car(); $bmw->name = 'BMW Series 3'; $bmw->setModelYear('2019'); print_r($bmw); /* Car Object ( [name] => BMW Series 3 [modelYear:Car:private] => 2019 ) */ ?>
Note: Methods can also be defined as private.
Private Methods
<?php class Test { /* Public method */ public function generateOrderDetails($userId) { $userInfo = $this->getUserInfo($userId); return $this->generateDetails($userInfo); } /* Private Method */ private function getUserInfo($userId) { //some logic to get user info } /* Private Method */ private function generateDetails($userInfo) { //some logic to generate details } } $testObj = new Test(); $testObj->generateOrderDetails($userId); //This is valid as generateOrderDetails method is public $testObj->getUserInfo($userId); //This is not allowed as getUserInfo is private $testObj->generateDetails($userInfo); //This is not allowed as generateDetails is private ?>
To demonstrate private methods, consider the code snippet above. We have a sample class with 2 private and 1 public method. We can only call the public method (
Constant Visibility
As of PHP 7.1, we can also define a Class Constant as public, private or protected. A constant defined without any explicit visibility is by default public. For instance, consider the example below.
<?php class Test { public const PUBLIC_CONSTANT = 'Public Constant'; private const PRIVATE_CONSTANT = 'Private Constant'; public function printConstants() { echo self::PUBLIC_CONSTANT; echo self::PRIVATE_CONSTANT; } } $testObject = new Test(); $testObject->printConstants(); //This works as the class method can access both private and public constants echo Test::PUBLIC_CONSTANT; //Works as the constant is public echo Test::PRIVATE_CONSTANT; //Fatal error as the constant is private ?>
Conclusion
In conclusion, we discussed Access Modifiers in this article. You should now understand the difference between public and private visibility access.
Read about Protected Access Modifier and Inheritance in PHP.
There is a lot to learn on PHP Official Documentation on Visibility.

Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.