Like other languages, PHP also implements some scope for the methods or attributes of the class. Access modifiers define where the class variables or methods can be accessed. In this article, you will learn about the access modifiers in OOP and their types.

Types of Access Modifiers in PHP

There are three access modifiers in PHP.

Public – When you create a method or attribute in the PHP class and do not specify the access modifier, it is set to public by default. In this mode, the method or attribute is accessible everywhere.

Private – When you create a method or variable with a private access modifier, it is accessible only within the class in which it is created.

Protected – Protected method or attribute is accessible within the class in which it is created. Also, the classes derived from this class can its a protected method and attribute.

Example

<?php
class Person{
  public $name;
  protected $age;
  private $height;
}

$x= new Person();
$x->name = 'Jawad'; // OK
$x->age= '32'; // ERROR
$x->height= '5.4'; // ERROR
?>

Explanation of the above example

  • In the above example, we create a class Person with three types of attributes with three different types of access modifiers.
  • The name attribute is set to public. Whereas, the age and height are set to protected and private respectively.
  • When we try to access the attribute name outside the class and assign some value to it, it works well.
  • But, when we try to assign value to the age and height attributes outside the class, it produce fatal error.

So, from the above example, you have a clear understanding of access modifiers in PHP. Let’s practice the methods.

Public, Private and Protected methods in PHP

<?php
class Person{
  public $name;
  public $age;
  public $height;

  function set_name($n) {  // a public function (default)
    $this->name = $n;
  }
  protected function set_age($a) { // a protected function
    $this->age = $a;
  }
  private function set_height($h) { // a private function
    $this->height = $h;
  }
}

$person = new Person();
$person->set_name('Jawad'); // OK
$person->set_age('26'); // ERROR
$person->set_height('5.4'); // ERROR
?>

Explanation of the above example

  • In the above example, we create a class Person with three types of methods with three different types of access modifiers.
  • The name method is set to public. Whereas, the age and height are set to protected and private respectively.
  • When we try to access the method name outside the class and assign some value to it, it works well.
  • But, when we try to assign value to the age and height methods outside the class, it produce fatal error.

Other than these three access modifiers, we have Abstract and Final access modifiers in PHP/. I the next section, you will learn them with examples.

Abstract Access modifier in PHP

Abstract access modifier can only apply to the class and methods. We cannot use abstracts with the class attributes.

Once we use abstract with the class, its object cannot be initiated because it doesn’t have any constructor.

Also, remember, if you don’t use an abstract access modifier with the class and declare some abstract method inside it. The class becomes abstract as a whole.

For instance

<?php

abstract class Bike {

    private $maxSpeed = 80;

    // Simple method
    public function drivingSpeed() {
        return "Driving at " . $this->maxSpeed . " kilometer/hour";
    }

    // Abstract method
    abstract public function drive();
}

class Police extends Bike {

    public function drive() {
        return "Driving out of speed limit.";
    }

}

$bike= new Police();
echo $bike->drivingSpeed();

?>
  • In the above example, we create an abstract class Bike that contains an abstract method drive().
  • We create another class which provides implementation of the abstract method drive.
  • As we cannot create object of the abstract class, we use the Police class.

Final Access Modifier in PHP

Just like the abstract access modifier, we cannot use the final access modifier in PHP with the attributes of the class. We can make the class or its methods final.

A class with a final keyword cannot be inherited by the other classes. Also, the method with the final access modifier cannot be overridden by the subclasses derived from this class.

For instance – Final Class in PHP

<?php

final class ParentClass {

    final function msg1() {
        echo "Method 1 of final class";
    }

    function msg2() {
        echo "Method 2 of final class";
    }
}

$obj1 = new ParentClass();
$obj1->msg1();
$obj1->msg2();

?>
  • In the above class, we create a final method msg1() and a simple method msg2().
  • We cannot override the final method msg1(). However, the methods msg2() can be overriden by the class inheriting it.
  • Try to practice this problem by creating a class that extends the Parent class. Implement the both methods and check which method produce error upon overriding.

Another Example – Final Method in PHP

<?php

class ParentClass {

    final function msg1() {
        echo "Final Method 1 of simple class";
    }

    function msg2() {
        echo "Simple method 2 of simple class";
    }
}

class ChildClass extends ParentClass {

    function msg1() {
        echo "Trying final method to override.";
    }

}

?>
  • In the above example, we create a non final or simple class having two methods. msg1() is a final method and msg2() is simple.
  • We create a Child Class that extends the Parent Class and try to override the final method msg1() of the parent class.
  • It will produce fatal error and you will not be able to override the final method of parent class.