What are Access Modifiers in PHP Tutorial Example

Reading Time: 9 minutes
542 Views

Inside this article we will see the concept i.e What are Access Modifiers in PHP Tutorial. Article contains the classified information about PHP Access Modifiers – Private, Public, Protected.

In PHP, access modifiers are keywords that are used to define the level of access to a class, property, or method. There are three types of access modifiers in PHP: public, protected, and private.

  • Public: A public member can be accessed from anywhere, both inside and outside of the class.
  • Protected: A protected member can only be accessed within the class and its subclasses.
  • Private: A private member can only be accessed within the class where it is defined.

By default, all members of a class in PHP are public, but you can use the keywords public, protected, and private to explicitly define the access level of a member.

It is considered best practice to use the appropriate access modifiers in order to encapsulate and protect the state and behavior of an object, and to make your code more maintainable and secure.

Read More: Top Javascript Frameworks in 2023 For Developers To Learn

Let’s get started.

What is Public PHP Access Modifier?

In PHP, the public access modifier is used to define a class property or method that can be accessed from anywhere, both within the class where it is defined and from outside of the class. This means that a public member can be accessed and modified by any code, regardless of where it is located.

For example, if you have a class with a public property, any code can read or write the value of that property. And, if you have a class with a public method, any code can call that method.

Program

Here’s an example of a class with public properties and methods:

<?php
  
class MyClass {
    public $myPublicProperty = "This is a public property";
    public function myPublicMethod() {
        echo "This is a public method";
    }
}

$myObject = new MyClass();

// Accessing public property and method directly
echo $myObject->myPublicProperty; // Output: This is a public property
$myObject->myPublicMethod(); // Output: This is a public method

As you can see, the public property myPublicProperty and method myPublicMethod() are accessible directly from anywhere, both within the class and outside of the class.

By default, if you don’t specify any access modifier for class property or method, it is considered as public.

What is Private PHP Access Modifier?

In PHP, the private access modifier is used to define a class property or method that can only be accessed within the class where it is defined. This means that a private member can only be accessed and modified by code that is located within the same class.

Read More: Do You Know Top PHP Frameworks in 2023 for Web Developers

For example, if you have a class with a private property, only code within the class can read or write the value of that property. And, if you have a class with a private method, only code within the class can call that method.

When a class member is defined as private, it is not visible or accessible to any code outside of the class. This allows for more encapsulation and protection of the state and behavior of an object, and makes the code more maintainable and secure.

Program

Here’s an example of a class with private properties and methods:

<?php
  
class MyClass {
    private $myPrivateProperty = "This is a private property";
    private function myPrivateMethod() {
        echo "This is a private method";
    }

    public function accessPrivate(){
        echo $this->myPrivateProperty;
        $this->myPrivateMethod();
    }
}

$myObject = new MyClass();

// Attempting to access private property and method directly
echo $myObject->myPrivateProperty; // Error: Undefined property
$myObject->myPrivateMethod(); // Error: Fatal error

// Accessing private property and method through a public method
$myObject->accessPrivate(); // Output: This is a private property This is a private method

As you can see, the private property myPrivateProperty and method myPrivateMethod() are not accessible directly from outside of the class, but they can be accessed by the public method accessPrivate() which is defined within the class.

What is Protected PHP Access Modifier?

In PHP, the protected access modifier is used to define a class property or method that can only be accessed within the class where it is defined, as well as any subclasses that inherit from that class. This means that a protected member can only be accessed and modified by code that is located within the same class or any of its child classes.

Read More: Laravel Migration Add Column After Specific Column Tutorial

For example, if you have a class with a protected property, only code within the class or any of its child classes can read or write the value of that property. And, if you have a class with a protected method, only code within the class or any of its child classes can call that method.

When a class member is defined as protected, it is not visible or accessible to any code outside of the class or its child classes. This allows for more encapsulation and protection of the state and behavior of an object, and makes the code more maintainable and secure.

Program

Here’s an example of a class with protected properties and methods:

<?php
  
class MyParentClass {
    protected $myProtectedProperty = "This is a protected property";
    protected function myProtectedMethod() {
        echo "This is a protected method";
    }
}

class MyChildClass extends MyParentClass {
    public function accessProtected(){
        echo $this->myProtectedProperty;
        $this->myProtectedMethod();
    }
}

$myParentObject = new MyParentClass();
$myChildObject = new MyChildClass();

// Attempting to access protected property and method directly from parent class
echo $myParentObject->myProtectedProperty; // Error: Undefined property
$myParentObject->myProtectedMethod(); // Error: Fatal error

// Attempting to access protected property and method directly from outside of child class
echo $myChildObject->myProtectedProperty; // Error: Undefined property
$myChildObject->myProtectedMethod(); // Error: Fatal error

// Accessing protected property and method through a public method of child class
$myChildObject->accessProtected(); // Output: This is a protected property This is a protected method

As you can see, the protected property myProtectedProperty and method myProtectedMethod() are not accessible directly from outside of the parent class or child class, but they can be accessed by the public method accessProtected() which is defined within the child class as child class inherits the property and method from parent class.

We hope this article helped you to learn about i.e What are Access Modifiers in PHP Tutorial Example in a very detailed way.

Read More: Laravel Change Table or Column Name with Data Type

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more