PHP OOP - Access Modifiers
PHP - Access Modifiers
Properties and methods can have access modifiers (or visibility keywords) which control where they can be accessed.
In PHP, there are three access modifiers:
-
public- the property or method can be accessed from everywhere. This is default -
protected- the property or method can be accessed within the class and by classes derived from that class -
private- the property or method can ONLY be accessed within the same class where they are defined
Note: If no acces modifier is specified, it will be set to
public.
Public Access Modifier
The public access modifier allows class properties or methods to be accessed
from everywhere.
In the following example, the $name property and the get_details() method are accessible from outside the class.
Example
<?php
class Fruit {
public $name;
public
function get_details() {
echo "Name: " . $this->name .
".";
}
}
$apple = new Fruit();
$apple->name = "Apple"; // Can be
accessed directly
$apple->get_details();
?>
Try it Yourself »
Private Access Modifier
The private access modifier allows class properties or methods
ONLY to be accessed within the same class where they are defined.
In the following example, the $name property is private and cannot be accessed directcly.
Example
<?php
class Fruit {
private $name;
public
function get_details() {
echo "Name: " . $this->name .
".";
}
}
$apple = new Fruit();
$apple->name = "Apple"; // Error:
Cannot access private property
$apple->get_details();
?>
Try it Yourself »
Protected Access Modifier
The protected access modifier allows class properties or methods
to be accessed within the class and by classes derived from that class (child
classes).
In the following example, the $name property is protected and cannot be accessed directcly.
Example
<?php
class Fruit {
private $name;
public
function get_details() {
echo "Name: " . $this->name .
".";
}
}
$apple = new Fruit();
$apple->name = "Apple"; // Error:
Cannot access protected property
$apple->get_details();
?>
Try it Yourself »
In the following example, the $name property is protected and cannot be accessed directcly from outside the class, but it will be accessible within the Apple subclass (child class).
Example
<?php
class Fruit {
protected $name;
public
function setType($name) {
$this->name = $name;
}
}
class
Apple extends Fruit {
public function getType() {
echo
"Name: " . $this->name . ".";
}
}
$apple = new Apple();
$apple->setType("Apple");
//echo $apple->name; // Error: Cannot access
protected property
echo $apple->getType(); // Output: Name: Apple.
?>
Try it Yourself »