php
  1. php-access-specifiers

PHP Access Specifiers

In PHP, access specifiers are used to set the level of visibility and access of properties and methods of a class.

Syntax

There are three types of access specifiers in PHP:

  1. Public - Public properties and methods are accessible from anywhere, both inside and outside the class.
class MyClass {
    public $myProperty = "Hello";

    public function myMethod() {
        echo "This is a public method.";
    }
}
  1. Protected - Protected properties and methods are accessible within the class and its child classes.
class MyClass {
    protected $myProperty = "Hello";

    protected function myMethod() {
        echo "This is a protected method.";
    }
}
  1. Private - Private properties and methods are only accessible within the class.
class MyClass {
    private $myProperty = "Hello";

    private function myMethod() {
        echo "This is a private method.";
    }
}

Example

Let's take an example to understand the concept of access specifiers in PHP.

class MyClass {
    public $publicProperty = "This is a public property.";

    protected $protectedProperty = "This is a protected property.";

    private $privateProperty = "This is a private property.";

    public function publicMethod() {
        echo "This is a public method.";
    }

    protected function protectedMethod() {
        echo "This is a protected method.";
    }

    private function privateMethod() {
        echo "This is a private method.";
    }

    public function showPropertiesAndMethods() {
        echo $this->publicProperty . "<br />";
        echo $this->protectedProperty . "<br />";
        echo $this->privateProperty . "<br />";
        $this->publicMethod();
        $this->protectedMethod();
        $this->privateMethod();
    }
}

$obj = new MyClass();
$obj->showPropertiesAndMethods();

Output

This is a public property.
This is a protected property.
This is a private property.
This is a public method.
This is a protected method.
This is a private method.

Explanation

In the example above, we have defined a class MyClass with three types of access specifiers - public, protected, and private. We have also defined methods to access these properties and methods.

When we create an object of the MyClass class and call the showPropertiesAndMethods method, it displays all the properties and methods of the class using their respective access specifiers.

Use

Access specifiers are used to control the visibility and accessibility of properties and methods of a class. They help in encapsulating the data, meaning we can prevent direct access to the internal state of an object.

Some possible use cases for access specifiers are:

  • Restricting access to sensitive data or methods
  • Defining a clean interface for using a class
  • Enforcing data consistency
  • Enabling inheritance and polymorphism

Important Points

  • Public properties and methods can be accessed from anywhere.
  • Protected properties and methods can only be accessed within the class and its child classes.
  • Private properties and methods can only be accessed within the class.
  • The default access specifier for properties and methods is public.

Summary

Access specifiers in PHP are used to control the visibility and accessibility of properties and methods of a class. They are defined using the public, protected, and private keywords. Public properties and methods are accessible from anywhere, protected properties and methods are accessible within the class and its child classes, and private properties and methods are only accessible within the class. Access specifiers help in encapsulating data, defining a clean interface, enforcing data consistency, and enabling inheritance and polymorphism.

Published on: