July 9, 2025

In object-oriented programming, public, private, and protected are access modifiers used to define the visibility and accessibility of class properties and methods?

  1. public: A public property or method is accessible from anywhere, both inside and outside the class. This means that any code can access and modify the value of a public property or call a public method.

Here is an example:

class Car {
    public $make;

    public function start() {
        // code to start the car
    }
}

$car = new Car();
$car->make = 'Toyota';
$car->start();

In this example, the make property and start() method are both declared as public, which means they can be accessed and modified by any code.

  1. private: A private property or method is only accessible within the class that defines it. This means that code outside the class cannot access or modify the value of a private property or call a private method.

Here is an example:

class Car {
    private $model;

    private function stop() {
        // code to stop the car
    }

    public function setModel($model) {
        $this->model = $model;
    }

    public function getModel() {
        return $this->model;
    }
}

$car = new Car();
$car->setModel('Camry');
echo $car->getModel(); // Output: Camry
$car->stop(); // This will result in a fatal error

In this example, the model property and stop() method are both declared as private, which means they can only be accessed and modified from within the Car class. However, the class provides two public methods setModel() and getModel() to allow external code to interact with the model property.

  1. protected: A protected property or method is accessible within the class that defines it and any subclasses that inherit from it. This means that code outside the class hierarchy cannot access or modify the value of a protected property or call a protected method.

Here is an example:

class Car {
    protected $year;

    protected function accelerate() {
        // code to accelerate the car
    }
}

class ElectricCar extends Car {
    public function start() {
        $this->accelerate();
    }

    public function setYear($year) {
        $this->year = $year;
    }
}

$car = new ElectricCar();
$car->setYear(2021);
$car->start();

In this example, the year property and accelerate() method are both declared as protected in the Car class. The ElectricCar class inherits from the Car class and can access and modify the year property and call the accelerate() method. However, external code cannot access or modify the year property or call the accelerate() method directly.

Public Members: Unless you specify otherwise, properties and methods of a class are public. That is to say, they may be accessed in three possible situations −

  1. From outside the class which it is declared
  2. From within the class in which it is declared
  3. From within another class that implements the class in which it is declared

Till now we have seen all members as public members. If you wish to limit the accessibility of the members of a class then you define class members as private or protected.

Private members: By designating a member private, you limit its accessibility to the class in which it is declared. The private member cannot be referred to from classes that inherit the class in which it is declared and cannot be accessed from outside the class.

A class member can be made private by using a private keyword infront of the member.

class MyTestClass {
private $car = “mercedes”;
$driver = “Prafulkr”;

function __construct($test) {
    // Statements here run every time an instance of the class is created.
}

function myTestPublicFunction() {
    return(“I’m visible!”);
}

private function myTestPrivateFunction() {
    return(“I’m in-visible outside!”);
}
}

When MyClass class is inherited by another class using extends, myPublicFunction() will be visible, as will $driver. The extending class will not have any awareness of or access to myPrivateFunction and $car, because they are declared private.

Protected members: A protected property or method is accessible in the class in which it is declared, as well as in classes that extend that class. Protected members are not available outside of those two kinds of classes. A class member can be made protected by using protected keyword in front of the member.

Here is a different version of MyClass:

class MyTestClass {
protected $car = “mercedes”;
$driver = “Prafulkr”;

function __construct($test) {
    // Statements here run every time an instance of the class is created.
}

function myTestPublicFunction() {
    return(“I’m visible!”);
}

protected function myTestPrivateFunction() {
    return(“I’m also visible in child class!”);
}
}

About The Author

Leave a Reply

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