Access Modifiers in PHP

In PHP, access modifiers like public, private, and protected control the visibility of class properties and methods. These modifiers are essential for data encapsulation and ensuring that code follows object-oriented programming (OOP) principles. In this guide, we’ll break down access modifiers step by step with examples and SEO-friendly keywords to improve your understanding.

Step 1: Introduction to Access Modifiers
The main purpose of access modifiers is to determine the accessibility of class members (properties and methods) in PHP. Here’s a quick overview of each:

Public: Members are accessible from anywhere, including outside the class, as long as you have an object reference.
Private: Members are accessible only within the class itself, not from outside or even through inheritance.
Protected: Members can be accessed within the class and by its child classes, but not from outside.
These access modifiers help implement data encapsulation, a key feature of OOP, where data members are hidden from the external environment, only allowing access through specific methods.

Step 2: Public Members in PHP
In PHP, if no access modifier is specified, the class members are public by default. This means they can be accessed freely from outside the class.

Example: Public Access Modifier

<?php
   class Book {
      /* Member variables */
      var $price;
      var $title;

      /* Constructor */
      function __construct(string $param1="PHP Basics", int $param2=380) {
         $this->title = $param1;
         $this->price = $param2;
      }

      function getPrice() {
         echo "Price: $this->price \n";
      }

      function getTitle() {
         echo "Title: $this->title \n";
      }
   }

   $b1 = new Book();
   echo "Title: $b1->title Price: $b1->price";
?>

Output

Title: PHP Basics Price: 380

Since the title and price properties are public, they are directly accessible outside the class.

Step 3: Private Members in PHP


The private access modifier restricts access to class members, allowing them to be accessed only within the class itself. This is essential for encapsulation, as it ensures that external code cannot modify or access these properties directly.

Example: Private Access Modifier

<?php
   class Book {
      private $price;
      private $title;

      /* Constructor */
      function __construct(string $param1="PHP Basics", int $param2=380) {
         $this->title = $param1;
         $this->price = $param2;
      }

      public function getPrice() {
         echo "Price: $this->price \n";
      }

      public function getTitle() {
         echo "Title: $this->title \n";
      }
   }

   $b1 = new Book();
   $b1->getTitle();
   $b1->getPrice();
   echo "Title: $b1->title Price: $b1->price"; // Error
?>

Output:

Title: PHP Basics
Price: 380
Fatal error: Cannot access private property Book::$title

In this case, direct access to the title and price properties results in an error because they are private. However, public methods can still access these private properties.

Step 4: Protected Members in PHP
The protected access modifier allows class members to be accessed within the class and any inherited classes, but not from outside.

Example: Protected Access Modifier

<?php
   class Book {
      private $price;
      protected $title;

      function __construct(string $param1="PHP Basics", int $param2=380) {
         $this->title = $param1;
         $this->price = $param2;
      }

      public function getPrice() {
         echo "Price: $this->price \n";
      }

      public function getTitle() {
         echo "Title: $this->title \n";
      }
   }

   class MyBook extends Book {
      public function displayTitle() {
         echo "Inherited Title: $this->title \n";
      }
   }

   $book = new MyBook();
   $book->displayTitle();
?>

Output:

Inherited Title: PHP Basics

The title property is protected, meaning it is accessible within the MyBook class, which inherits from the Book class. However, it wouldn’t be accessible from outside.

Step 5: Differences Between Public, Private, and Protected

Access ModifierAccess from ClassAccess from Child ClassAccess from Outside
PublicYesYesYes
PrivateYesNoNo
ProtectedYesYesNo

Choosing the Right Access Modifier in PHP

Access modifiers are crucial for implementing data encapsulation and controlling the visibility of class members in PHP. By understanding when to use public, private, or protected modifiers, you can ensure that your PHP classes are secure, maintainable, and follow OOP principles.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *