PHP OOP access modifiers are used to determine the visibility of class members inside or outside of the class & basically, it is used by class members such as properties and methods.
PHP object-oriented programming provides three types of access modifiers. These are:
Public access modifiers can be accessed within the class as well outside of the class.
Source Code
<?php
class Car {
//private
private $model;
public function getModel(){
return "The car model is " . $this -> model;
}
}
$mercedes = new Car();
// We try to access a private property from outside the class.
$mercedes -> model = "Mercedes benz";
echo $mercedes -> getModel();
//explain if you like
?>
A protected access modifier can be accessed inside the class as well as it will be accessed by subclasses too.
Source Code
<?php
class Demo{
protected $x=500;
protected $y=500;
function add(){
echo $sum=$this->x+$this->y."<br/>";
}
}
class Child extends Demo{
function sub(){
echo $sub=$this->x-$this->y."<br/>";
}
}
$obj= new Child;
$obj->add();
$obj->sub();
?>
Private access modifier can be access only within the class. It can not be access outside of the class.
Source Code
<?php
class Car {
//private
private $model;
public function getModel(){
return "The car model is " . $this -> model;
}
}
$mercedes = new Car();
// We try to access a private property from outside the class.
$mercedes -> model = "Mercedes benz";
echo $mercedes -> getModel();
?>
Private access modifier can be accessed through the following methods.
Source Code
<?php
class Car {
//the private access modifier denies access to the method from outside the class’s scope
private $model;
//the public access modifier allows access to the method from outside the class
public function setModel($model)
{
$this -> model = $model;
}
public function getModel()
{
return "The car model is " . $this -> model;
}
}
$mercedes = new Car();
//Sets the car’s model
$mercedes -> setModel("Mercedes benz");
//Gets the car’s model
echo $mercedes -> getModel();
?>
Access modifier is used to limit the class members accessing from outside of the class. After defining a property or method as private, only methods that are within the class are allowed to access it. So, in order to interact with private methods and properties, we need to provide public methods.
Source Code
<?php
class Car {
//the private access modifier denies access to the property from outside the class’s scope
private $model;
//the public access modifier allows access to the method from outside the class
public function setModel($model)
{
//validate that only certain car models are assigned to the $carModel property
$allowedModels = array("Mercedes benz","BMW");
if(in_array($model,$allowedModels))
{
$this -> model = $model;
}
else
{
$this -> model = "not in our list of models.";
}
}
public function getModel()
{
return "The car model is " . $this -> model;
}
}
$mercedes = new Car();
//Sets the car’s model
$mercedes -> setModel("Mercedes benz");
//Gets the car’s model
echo $mercedes -> getModel();
?>