Thursday 28 March 2019

Access Modifiers in PHP

Access modifiers: 

These are used to set access rights with classes and their members(methods or properties) defined within the class.

Possible Access Modifiers

Private methods or properties can only be used from inside the class. This protects members from outside class access.

Public methods or properties can be accessed from both the inside and outside of the class.

Protected modifier: which allows code usage from both inside the class and from its child classes.

Abstract: This is used only for PHP classes and its methods. An abstract class can contain abstract methods. Non abstract class can not contain abstract methods.

Final: It prevents subclasses to override parent class members with final keyword.

The public access modifier

<?php
class Car {

  // public methods and properties.
  public $model;    

  public function getModel() {
    return "The car model is " . $this -> model;
  }
}

$ferrari = new Car();
//Here we access a property from outside the class
$ferrari -> model = "Ferrari";
//Here we access a method from outside the class
echo $ferrari -> getModel();

Result: The car model is Ferrari

The private access modifier

Example, $model is private property. While trying to set its value from outside the class, we encounter a fatal error.

<?php

class Car {

  //private property
  private $model;
    
  public function getModel() {
    return "The car model is " . $this -> model;
  }
}

$ferrari = new Car();

// We try to access a private property from outside the class.
$ferrari -> model = "Ferrari";
echo $ferrari -> getModel();

Result: Fatal error: Cannot access private property Car::$model

Access a private property?

As we cannot access private properties from outside the class but still we have to set & get the properties's values.

To interact with private properties, use public methods because public methods can only interact with both the code outside as well as inside the class. These public methods are of two kinds:
Setters: This sets the value of private properties.
Getters: This gets the value of the private properties.

Example: In this example, $carModel is private property. With the use of setter and getter methods, set and get value of private property.
To set car model value use setModel() and for get car model value use getModel() method.

<?php

class Car {

  //the private access modifier denies access to the method from outside the class’s scope
  private $model;

  //the public access modifier allows the access to the method from outside the class
  public function setModel($model) {
    $this -> model = $model;
  }
  
  public function getModel() {
    return "The car model is  " . $this -> model;
  }
}

$ferrari = new Car();
//Sets the car’s model
$ferrari -> setModel("Ferrari");
//Gets the car’s model
echo $ferrari -> getModel();

Result: The car model is Ferrari

Why we need access modifiers?

To limit the modifications to class's methods and properties done by the code outside of the class.
When we define any method or property as private, only methods within the class can use these private methods or properties. So to interact with private methods and properties, we need to create public methods. Inside these public methods, put your logic to validate and restrict data that comes from outside of the class.

In our example, we can validate that only certain car models can make their way, and be assigned to the private $model property, by defining the allowed alternatives for models in the public setModel() method. For this purpose, we define inside the setModel() method an array of allowed car models, and check that only these models are assigned to the $model property.

<?php
class Car {

  //the private access modifier denies access to the property from outside the class’s scope
  private $model;

  //the public access modifier allows the access to the method from outside the class
  public function setModel($model) {
    //validate that only certain car models are assigned to the $carModel property
    $allowedModels = array("Mercedes benz","BMW", "Ferrari");

    if(in_array($model,$allowedModels)) {
      $this -> model = $model;
    }
    else {
      $this -> model = "not in our list of models.";
    }
  }
  
  public function getModel() {
    return "The car model is  " . $this -> model;
  }
}


$ferrari = new Car();
//Sets the car’s model
$ferrari -> setModel("Ferrari");
//Gets the car’s model
echo $ferrari -> getModel();



No comments:

Post a Comment