Categories
PHP

OOP Encapsulation and Visibility Modifiers

Encapsulation is just wrapping some data in an object. The term “encapsulation” is often used interchangeably with “information hiding”. PHP provides three visibility modifiers that you can use to control who can view and modify member variables as well as call member functions.

Encapsulation

Encapsulation is just wrapping some data in an object. The term “encapsulation” is often used interchangeably with “information hiding”.

Hiding the members of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.

You can use encapsulation if the properties of an object are private, and the only way to update them is through public methods.

For example, in this code, we can protect our User objects from setting an invalid gender parameter.

class User {
 private $name;
 private $gender;

 public function getName() {
  return $this->name;
 }

 public function setName($name) {
  $this->name = $name;
  return $this;
 }

 public function getGender() {
  return $this->gender;
 }

 public function setGender($gender) {
  if ('male' !== $gender and 'female' !== $gender) {
   throw new Exception('Set male or female for gender');
  }
  $this->gender = $gender;
  return $this;
 }
}

$user = new User();
$user->setName('Michal');
$user->setGender('male');

In the above example, we should assign male or female value for setGender method or it will print Fatal error: Uncaught exception 'Exception' with message 'Set male or female for gender'.

Access Modifiers

Three visibility (access) modifiers are provided:

  1. public
  2. protected
  3. private

Public visibility

public Anybody may read and modify this member variable or call this method. This modifier can be used by external code, from code within the given class, or in classes that extend the functionality of the given class.

Consider the following example, you can modify the $username property from the outside of the class because its visibility is public:

<?php
 class User {
  public $username;
  public function  __construct($u = 'Guest') {
   $this->username = $u;
  }
 }
 
 $user = new User();
 echo $user->username; # Prints: Guest

 #Modifying the public variable
 $user->username = 'BrainBell';
 echo $user->username; # Prints: BrainBell

Protected visibility

protected External code and other classes that have no relation to the given class can not read/modify member variables (properties) and functions (methods) with the protected keyword. However, classes that extend the functionality of this class are allowed to access or call it.

Consider the following example, we’ve changed the $username visibility from public to protected and when we’ve tried to modify its value from outside of the class, the code failed:

<?php
 class User {
  protected $username;
  public function  __construct($u = 'Guest') {
   $this->username = $u;
  }
 }
 
 $user = new User();
 $user->username = 'BrainBell';
 # Fatal error: Uncaught Error:
 # Cannot access protected property

Note: the protected methods and protected variables are accessible by all class members, consider the following example, we created a new function getUsername() that returns the protected variable $username which is not directly accessible but a class member can access it:

<?php
 class User {
  protected $username;
  public function  __construct($u = 'Guest') {
   $this->username = $u;
  }
  public function getUsername() {
   return $this->username;  
  }  
 }
 
 $user = new User();
 echo $user->getUsername(); # Prints: Guest

Private visibility

private Only code within this class may read and modify private variables or call private member functions. No other codes, classes, or classes that extend the functionality of this class may use private items.

Consider the following example, first, we’ve changed the $usename visibility to private, and then we’ve created a new class UserExt by extending the User class:

<?php
 class User {
  private $username;
  public function  __construct($u = 'Guest') {
   $this->username = $u;
  }
  public function getUsername() {
   return $this->username;  
  }  
 }
 
 class UserExt extends User {
  public function username(){
	return $this->username;
  }
 }
 $user = new UserExt();
 echo $user->username();
 # Warning: Undefined property: UserExt::$username

The UserExt->username() function can not access its parent class User’s private variable $username.


PHP OOP Tutorials: