Php

PHP Access Modifiers – Public, Private and Protected

In this tutorial, we are going to learn about the Complete Tutorials of PHP Access Modifiers with Example. To set the access rights for class methods and variables we use access modifiers which are nothing but PHP keywords.

There are three visibility levels, known as public, private, and protected. There are the following PHP keywords which are used as access modifiers along with their meaning:

  • Private: Members declared as private are restricted to access within the class itself.
  • Protected: Similar to private, but accessible by subclasses (covered in future lessons on inheritance).
  • Abstract: Used exclusively for PHP classes and their member functions.
  • Final: Methods marked as final within a class cannot be altered or overridden by any subclass.

Access Modifiers in PHP

The Access specifiers specify the level of access that the outside world have on the class methods and class data members. The Access specifiers can either be public, private, or protected.

You can also check other tutorials of PHP,

Why do we need Access specifiers:

Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.

Explanation of each access specifier

  1. Private
  2. Protected
  3. Public

1. Private

A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:

class Customer {  
private $name;   
public function setName($name) {  
 $this->name = $name;  
}  
  
public function getName() {  
 return $this->name;  
}  
}  
  
$c = new Customer();  
$c->setName(“Stuart Broad”);  
echo $c->name; //error, $name cannot be accessed from outside the class  
//$name can only be accessed from within the class  
  
echo $c->getName(); //this works, as the methods of the class have access  
//to the private data members or methods

In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.

Related Post

2. Public

A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:

class Customer {  
public $name;   
public function setName($name) {  
  $this->name = $name;  
}  
  
public function getName() {  
  return $this->name;  
}  
}
$c = new Customer();  
$c->setName(“Stuart Broad”);  
echo $c->name;    // this will work as it is public.  
$c->name = “New Name” ; // this does not give an error.

In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.

3. Protected

A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and its child class; but is private for the rest of the program (outside world).

Look at the example below:

class Customer {  
protected $name;  
  
public function setName($name) {  
$this->name = $name;  
}  
  
public function getName() {  
return $this->name;  
}  
}  
  
class DiscountCustomer extends Customer {  
  
private $discount;  
  
public function setData($name, $discount) {  
$this->name = $name; //this is storing $name to the Customer  
//class $name variable. This works  
// as it is a protected variable  
  
$this->discount = $discount;  
}  
}  
  
$dc = new DiscountCustomer();  
$dc->setData(“Stuart Broad”,10);  
echo $dc->name; // this does not work as $name is protected and hence  
// only available in Customer and DiscountCustomer class

In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.

Important Note of Access Specifier in PHP7

In PHP5/7, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories