What Access Modifiers in php ?

The php access modifiers have the power to set the access to the classes and their members that is  the functions or variables  which is defined with in the class scope .

Also in php there are some keywords representing the access modifiers .

Now let us have look at the possible keywords and  access modifiers .

  • Public –  The public access modifiers of class and its member function are publicly accessible from anywhere ,even from the outside of the scope of the class .
  • Private – The Private access modifiers are only accessible within the class itself . it protects the access of the member functions from out side of the class with the reference if the class instance .
  • Protected –  The protected access modifiers are similar to the private ,except it allows to access its member functions to its child or sub classes .
  • Abstract – The abstract key word is only used for the the class and its member functions. in order to contain the abstract function php class should have the abstract keywords or abstract class .
  • Final – The final keyword prevents the subclass to override the super class member function . 
Access Modifiers usage with PHP class and and its member functions .

The Applicable for the access modifiers with in the class and its member functions are listed below :

access modifier classes functions variable
public Not Applicable Applicable Applicable
private Not Applicable Applicable Applicable
protected Not Applicable Applicable Applicable
abstract Applicable Applicable Not Applicable
final Applicable Applicable Not Applicable

Public Access modifiers :

Before specifying any access modifiers in the php all the class and member are treated as the public by default.
The php class is treated as public although we don’t specify any access modifiers to it .
If we specify any access modifiers to the php class such as public,private or protected it will produce a parsing error .
For example : 
we are creating class with public access modifier ,
public class phpinterviewquestions (){
…………….
……………
}
Than the parsing error will be generated as

Parse error: syntax error, unexpected ‘public’ (T_PUBLIC) in …

In the same way the class function is treated as public if we don’t specify any access modifiers to it . But as a good programming practice would be better to assign the access modifiers as public , private protected etc…

Unlikely as the member function you need to assign the access modifiers to the class variables .

As for example 
<?php
class php_programming {
 $app = array(‘magento’ , ‘wordpress’ , ‘drupal’ , ‘joomla’ );
}
?>

The above method of declaration of the variable are invalid and will produce the parse error .
The best way of declaration is as given below .

<?php
class php_programming {
 public $app = array(‘magento’ , ‘wordpress’ , ‘drupal’ , ‘joomla’ );
}
?>

Private Access modifiers :

We can assign this variable only to Class member functions and its variables but not to the class itself as like as the public access modifiers .The php class function defined as the private cannot be accessed directly through the instance of the class .

As For Example :

class phpProgramming {
private $apps = array(‘magento’,’wordpress’,’drupal’,’joomla’,’wordpress’);

public function appCats() {
return $this->apps;
}
}
$objPHP = new phpProgramming();

print_r($objPHP->app); // invalid Call
print_r($objPHP->appCats());

In the above example calling the private variables directly via class instance is the wrong way and produce an Fatal error as

Fatal error: Cannot access private property phpProgramming::$app in …

But we can access the private variables  via class instance passing/call  through the public functions of the class as

print_r($objPHP->appCats());

will produce an out put as

Array ( [0] => magento [1] => wordpress [2] => drupal [3] => joomla [4] => wordpress )

Note : we can access all the member variables and and function with in the class using $this function.

Protected Access modifiers :

Generally , this keyword in used in PHP inheritance in the php program . it helps to prevent the access of the php class itself and its member functions except with in the class or its child classes.

With the help of this class, the php class and its members can’t be able to access directly from outside class and and their child classes , with the reference to there inheritance .

Let see with example :

<?php
class Program {
protected $language = array(“php”,”java”,”dotnet”);

protected $uses = array(array(“name”=>”Mechanical Cars”,”category”=>”pull back”),
array(“name”=>”Magento”,”category”=>”php”),
array(“name”=>”Drupal”,”category”=>”php”),
array(“name”=>”Jsp”,”category”=>”java”),
array(“name”=>”Window applications”,”category”=>”dotnet”),
array(“name”=>”Wordpress”,”category”=>”php”));

protected function getUses() {
for($i=0;$i<count($this->uses);$i++) {
$usesList[] = $this->uses[$i][“name”];
}
return $usesList;
}
protected function getUsesForLanguage($category) {
for($i=0;$i<count($this->uses);$i++) {
if($this->uses[$i][“category”]==$category) 
$usesList[] = $this->uses[$i][“name”];
}
return $usesList;
}
}

class ChildProgram extends Program {
protected $category = “php”;
function getUses() {
return $this->getUsesForLanguage($this->category);
}
}

$objProgram = new Program();
$objChildProgram = new ChildProgram();
print “<pre>”;
/**Invalid
print_r($objProgram->language);
print_r($objChildProgram->language);
print_r($objProgram->getUses());*/
print_r($objChildProgram->getUses());
print “</pre>”;
?>

In the above program you can see that the statements are commented . The commented statements are invalid attempts to call the protected member of class from the outside of the Program class.

It was possible to access the members function as done with private members and access through the public functions of the class .

Abstract Access modifiers :

The abstract can be defined to PHP classes and its member function but we cannot define the abstract to the member variables. For having the abstract members functions the class should be defined as the abstract.
The PHP abstract classes and its member function cannot be accessed directly via its instance because php does’t allows to create the instance of the abstract class.
Although if we try to instance and access the PHP abstract class and its member functions it will produce the fatal error …

Fatal error: Cannot instantiate abstract class …

But the abstract class can be inherited or simply you can say that it act like PHP interface . The abstract function are just a  declaration rather it doesnot contains the function body and we can inherit the abstract class via non-abstract subclass with the help of defining the same function as defined in the abstract class as abstract function .

For Example :

<?php
abstract class Program {
public $language = array(“php”,”java”,”dotnet”);

public $uses = array(array(“name”=>”Mechanical Cars”,”category”=>”pull back”),
array(“name”=>”Magento”,”category”=>”php”),
array(“name”=>”Drupal”,”category”=>”php”),
array(“name”=>”Jsp”,”category”=>”java”),
array(“name”=>”Window applications”,”category”=>”dotnet”),
array(“name”=>”Wordpress”,”category”=>”php”));

abstract function getUses();

protected function getUsesForLanguage($category) {
for($i=0;$i<count($this->uses);$i++) {
if($this->uses[$i][“category”]==$category) 
$usesList[] = $this->uses[$i][“name”];
}
return $usesList;
}
}

class ChildProgram extends Program {

protected $category = “php”;
function getUses() {
return $this->getUsesForLanguage($this->category);
}
}

$objChildProgram = new ChildProgram();
print “<pre>”;
print_r($objChildProgram->getUses());
print “</pre>”;
?>

Output :

Array
(
[0] => Magento
[1] => Drupal
[2] => WordPress
)

In above example , you can see that  class Program is defined as abstract class as well as function getUses() is also abstract .And in child class  ChildProgram , the function of abstract class getUsesForLanguage() is inherited with the help of getUses() which is defined as abstract in parent class .

Final Access modifiers :

The final access modifiers prevents the class from inheritance. from its child classes as well as other class . The member functions and variables cannot be inheritance .
The final class should be used with the class and its function , it is not applied to class variables .
The final class cannot be overridden .
If you try the inheritance the final class it will generate the following Fatal error..

Fatal error: Class SoftToys may not inherit from final class …


Leave a Reply

Your email address will not be published.Required fields are marked *