Access Specifiers

It specify the level of access to the the data member or function in a class

Please note that all data members and member functions are public by default.


A private access specifier is used to hide data and member functions. A method or data member declared as private can only be accessed by the class itself and neither the outside program nor the derived class can have access to it.


A public access specifier allows the data members and methods to be access from anywhere in the script. Lets look at an example below:


A protected access specifier allows the derived class to access the data member or member functions of the base class, whereas disallows global access to other objects and functions.

class PHPCode{
private $a;
public $b;
public function __construct($a, $b) {
$this->b = $a;
$this->b = $b;

$obj = new PHPCode(7,17);
echo $obj->b;
Here we can not access the variable a but b

The reason why data members are declared private is to avoid the outside programs to either accidentally modify the values without necessary validation.

When you define a method as private that method can only be called from within that class and not from outside that is the global level script.

Private data member or functions can not be accessed by the child class objects