How To Use Access Modifiers In PHP

Today we will learn OOPS which is a very important concept used in any program. Every language like PHP,.NET, JAVA ,etc. uses OOPS concepts. OOPS stands for object oriented programming structure. OOPS depends on class and object. Properties use ‘access modifiers’ in oops.

There are 3 access modifiers,
  1. public
  2. private
  3. protected
Public

Public classes are those classes which can refer to field or method. No restriction is there for any individual to access.

Syntax
  1. <?php  
  2. class Member {  
  3.    
  4.   public $username = "";  
  5.   private $loggedIn = false;  
  6.    
  7.   public function login() {  
  8.     $this->loggedIn = true;  
  9.   }  
  10.    
  11.   public function logout() {  
  12.     $this->loggedIn = false;  
  13.   }  
  14.    
  15.   public function isLoggedIn() {  
  16.     return $this->loggedIn;  
  17.   }  
  18. }  
  19.    
  20. class Administrator extends Member {  
  21.    
  22.   public function createForum( $forumName ) {  
  23.     echo "$this->username created a new forum: $forumName<br>";  
  24.   }  
  25.    
  26.   public function banMember( $member ) {  
  27.     echo "$this->username banned the member: $member->username<br>";  
  28.   }  
  29.    
  30. }  
  31.   
  32.   
  33. // Create a new member and log them in  
  34. $member = new Member();  
  35. $member->username = "Fred";  
  36. $member->login();  
  37. echo $member->username . " is " . ( $member->isLoggedIn() ? "logged in" : "logged out" ) . "<br>";  
  38.    
  39.   
  40. ?>  
Private

Private classes are those classes in which only the current class has acess to its fields and methods.
  1. <?php  
  2.   class WebDeveloper {  
  3.     private $name;  
  4.     private function __construct($name) {  
  5.       $this->name = $name  
  6.     }  
  7.   }  
  8.   $developer = new WebDeveloper('jordizle');  
  9.   echo $developer->name;  
  10. ?>  

The above program will show an error because the attribute is set to private. To access the attributes the class should have a public method that can return the value. 

  1. <?php  
  2. class WebDeveloper {  
  3.   private $name;  
  4.   private function __construct($name) {  
  5.     $this->name = $name  
  6.   }  
  7.   public function getName() {  
  8.     return $this->name;  
  9.   }  
  10. }  
  11.   
  12. $developer = new WebDeveloper('jordizle');  
  13.   echo $developer->getName();  
  14. ?>  
Protected

Protected classes are those classes in which only the current class and subclass of this class would have access to the field or method.
  1. <?php  
  2.     class dog {  
  3.         public $Name;  
  4.         protected function getName() {  
  5.             return $this->Name;  
  6.         }  
  7.     }  
  8.   
  9.     class poodle extends dog {  
  10.         public function bark() {  
  11.             print "'Woof', says " . $this->getName();  
  12.         }  
  13.     }  
  14.       
  15.     $poppy = new poodle;  
  16.     $poppy->Name = "Poppy";  
  17.     $poppy->bark();  
  18. ?>  
Abstract Class

An abstract class is a class that contains at least one abstract method.This is partially implemented by the programmer. An abstract method is just a function definition that tells the programmer that the method must be implemented in the child class. It can contain one or more abstract methods.

An abstract class is created by using abstract keyword. If we have made any class abstract then we cannot further create any object in that class. If a function is defined as private then it cannot be declared as private. It can only be declared as public and protected.
  1. <?php   
  2.     abstract class employee  
  3.    {  
  4.        protected $empname;  
  5.        protected $empage;  
  6.    
  7.        function setdata($empname,$empage)  
  8.       {  
  9.           $this->empname = $empname;  
  10.           $this->empage = $empage;  
  11.       }  
  12.    
  13.       abstract function outputData();  
  14.    }  
  15.    
  16.    class EmployeeData extends employee   //extending abstract class  
  17.    {  
  18.         function __construct($name,$age)  
  19.         {  
  20.            $this->setdata($name,$age);  
  21.         }  
  22.    
  23.        function outputData()  
  24.        {  
  25.           echo $this->empname;  
  26.           echo $this->empage;  
  27.        }  
  28.    }  
  29.    
  30.     $a = new EmployeeData("Hitesh","24");  
  31.     $a->outputData();  
  32. ?>  
Interface Class

Interface class doesn’t add any additional functionality but it outlines a standard format which your classes need to use. Any class that inherits from Interface Class must have the same methods as the interface class.

Interface classes are defined by using the ‘Interface’ Keyword. The nature of inheritance is always public; i.e, all the methods In the interface must be public.

Syntax
  1. interface iTemplate  
  2. {  
  3.     public function setVariable($name$var);  
  4.     public function getHtml($template);  
  5. }    
  6. class Template implements iTemplate  
  7. {  
  8.     private $vars = array();  
  9.     
  10.     public function setVariable($name$var)  
  11.     {  
  12.         $this->vars[$name] = $var;  
  13.     }  
  14.     
  15.     public function getHtml($template)  
  16.     {  
  17.         foreach($this->vars as $name => $value) {  
  18.             $template = str_replace('{' . $name . '}'$value$template);  
  19.         }  
  20.    
  21.         return $template;  
  22.     }  
  23. }  
  24.   
  25.   
  26.   
  27. // This will not work  
  28. // Fatal error: Class BadTemplate contains 1 abstract methods  
  29. // and must therefore be declared abstract (iTemplate::getHtml)  
  30.   
  31. class BadTemplate implements iTemplate  
  32. {  
  33.     private $vars = array();  
  34.     
  35.     public function setVariable($name$var)  
  36.     {  
  37.         $this->vars[$name] = $var;  
  38.     }  
  39. }  
  40. ?>  
Final Class

Final Class can’t be extended. A
final keyword is used before the class to which you want to declare Final.
  1. final class BaseClass {  
  2.    public function myMethod() {  
  3.       echo "BaseClass method called";  
  4.    }  
  5. }  
  6.    
  7. //this will cause Compile error  
  8. class DerivedClass extends BaseClass {  
  9.    public function myMethod() {  
  10.       echo "DerivedClass method called";  
  11.    }  
  12. }  
  13.    
  14. $c = new DerivedClass();  
  15. $c->myMethod();  
Here in the above code snippet we are seeing that baseclass is declared as final and thus it can’t be extended (inherited). DerivedClass tries to extend from BaseClass, therefore the compiler will throw a compile error.


Similar Articles