What is Access Modifiers in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023


In this tutorial we will learn what is access modifier in Laravel.


What is access modifier in Laravel

Access modifiers in Laravel are keywords that are used to restrict the visibility and accessibility of class properties and methods. In Laravel, there are three access modifiers:

1) public: A public property or method can be accessed from anywhere, both inside and outside the class.


2) protected: A protected property or method can only be accessed within the class or its child classes.


3) private: A private property or method can only be accessed within the class.

By default, all class members are public. However, it is good practice to explicitly specify the access modifiers for each property and method to ensure proper encapsulation and prevent unauthorized access or modification.

For example, consider the following class:

class MyClass {
    public $publicProperty;
    protected $protectedProperty;
    private $privateProperty;
    public function publicMethod() {
        // code
    protected function protectedMethod() {
        // code
    private function privateMethod() {
        // code


In this example, publicProperty and publicMethod() are accessible from anywhere, protectedProperty and protectedMethod() are accessible within the class or its child classes, and privateProperty and privateMethod() are only accessible within the class.

Comments 0

Leave a comment