Home > selvabalaji > PHP Access Modifiers

PHP Access Modifiers

PHP access modifiers are used to set access rights with PHP classes and their members that is the functions and variables defined within the class scope. In PHP, there are some keywords representing these access modifiers. These keywords will be added with PHP classes and its members.

PHP keywords as Access Control Modifiers

Now, let us have a look into the following list to know about the possible PHP keywords used as access modifiers.

  1. public – class or its members defined with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.
  2. private – class members with this keyword will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.
  3. protected – same as private, except by allowing sub classes to access protected super class members.
  4. abstract – This keyword can be used only for PHP classes and its functions. For containing abstract functions, a PHP class should be an abstract class.
  5. final – It prevents sub classes to override super class members defined with final keyword.

Access modifiers Usage with PHP Classes, Class Members

Based on the possibility of applying the above list of PHP access modifiers, we can classify them. The following tabular column specify which keyword could be applied where in classes,functions or methods.

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 Modifier

Before introducing access modifiers in PHP, all classes and its members are treated aspublic by default. Still, for PHP classes without specifying any access modifiers, then it will be treated as public.

If we specify public,private or protected for a PHP class, then it will cause parse error. For example, if we define a class as,

public class  {
...
...
}

Then, the following parse error will be displayed to the browser.

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in ...

Similarly, class functions without any access modifiers, will be treated as public functions. But, it is good programming practice to specify those functions as public for better understanding.

Unlike PHP classes and functions, we need to specify access modifiers for PHP class variables explicitly. For example, the following code is invalid, which will cause PHP parse error.

class Books {
$categories = array("puzzles","pull back","remote","soft");
...
}

And, the valid code for defining classes with public variables are follows.

class Books {
public $categories = array("puzzles","pull back","remote","soft");
...
}

private Access Modifier

We can state this keyword only for class variables and function, but not for class itself, as like as public modifier. PHP class members defined as private cannot be accessed directly by using its instance. For example,

class Books {
private $categories = array("puzzles","pull back","remote","soft");

public function getbooksCategories() {
return $this->categories;
}
}
$objbooks = new Books();
print "<pre>";
print_r($objbooks->categories); // invalid
print_r($objbooks->getbooksCategories());
print "</pre>";

In the above program, Books class contains an private variable and a public function. Accessing this private variable by using Books class instance with the line,

print_r($objbooks->categories); // invalid

will cause PHP fatal error as,

Fatal error:  Cannot access private property Books::$categories in ...

But we can access class private variables via any public function of that class. In the above program we are getting the elements of $categories array variables via getbooksCategories() function defined as public.

Note that, all members of a class can be access within the class scope by using $this variable.

protected Access Modifier

This keyword is used in a PHP program which is using PHP inheritance. And, it is used to prevent access for PHP classes and its members from anywhere, except from inside the class itself or from inside its subclasses.
With this keyword, PHP classes and its members functions cannot be access directly from outside the classes and their subclasses, with the references of their instance.

Consider the following PHP program.

<?php
class Books {
protected $categories = array("puzzles","pull back","remote","soft");
protected $books = array(array("name"=>"Mechanical books","category"=>"pull back"),
array("name"=>"Jigsaw","category"=>"puzzles"),
array("name"=>"HiTech","category"=>"mech"),
array("name"=>"Teddy","category"=>"soft"),
array("name"=>"Baby","category"=>"soft"),
array("name"=>"Chinese","category"=>"puzzles"),
array("name"=>"Electronic","category"=>"remote"));
protected function getBooks() {
for($i=0;$i<count($this->nooks);$i++) {
$books_list[] = $this->books[$i]["name"];
}
return $books_list;
}
protected function getBooksByCategory($category) {
for($i=0;$i<count($this->books);$i++) {
if($this->books[$i]["category"]==$category) 
$books_list[] = $this->books[$i]["name"];
}
return $books_list;
}
}

class SoftBooks extends Books {
protected $category = "soft";
function getBooks() {
return $this->getBooksByCategory($this->category);
}
}

$objBooks = new Books();
$objSoftBooks = new Softbooks();
print "<pre>";
/**Invalid
print_r($objBooks->categories);
print_r($objSoftBooks->categories);
print_r($objBooks->getBooks());*/
print_r($objSoftBooks->getBooks());
print "</pre>";
?>
Categories: selvabalaji
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: