0
0
PHPprogramming~5 mins

Access modifiers (public, private, protected) in PHP

Choose your learning style9 modes available
Introduction

Access modifiers control who can use or change parts of your code. They help keep your code safe and organized.

When you want everyone to use or see a part of your code.
When you want only the code inside a class to use or change something.
When you want only the class and its child classes to access a part of the code.
When you want to hide details that should not be changed directly.
When you want to protect important data from accidental changes.
Syntax
PHP
class ClassName {
    public $publicVar;
    private $privateVar;
    protected $protectedVar;

    public function publicMethod() {}
    private function privateMethod() {}
    protected function protectedMethod() {}
}

public means anyone can access.

private means only inside the class can access.

protected means inside the class and child classes can access.

Examples
This class shows all three access modifiers on properties and methods.
PHP
<?php
class Car {
    public $color;
    private $engineNumber;
    protected $speed;

    public function setColor($c) {
        $this->color = $c;
    }

    private function setEngineNumber($num) {
        $this->engineNumber = $num;
    }

    protected function setSpeed($s) {
        $this->speed = $s;
    }
}
Protected method greet is used inside child class method sayHello.
PHP
<?php
class ParentClass {
    protected function greet() {
        echo "Hello from Parent\n";
    }
}

class ChildClass extends ParentClass {
    public function sayHello() {
        $this->greet();
    }
}

$child = new ChildClass();
$child->sayHello();
Private property secret is accessed only inside the class via a public method.
PHP
<?php
class Example {
    private $secret = "hidden";

    public function reveal() {
        return $this->secret;
    }
}

$obj = new Example();
echo $obj->reveal();
Sample Program

This program shows how public, private, and protected properties and methods work. Public is accessed directly, private only inside the class, and protected inside the class and child classes.

PHP
<?php
class Box {
    public $width;
    private $height;
    protected $depth;

    public function __construct($w, $h, $d) {
        $this->width = $w;
        $this->height = $h;
        $this->depth = $d;
    }

    public function getHeight() {
        return $this->height;
    }

    protected function getDepth() {
        return $this->depth;
    }
}

class BigBox extends Box {
    public function showDepth() {
        return $this->getDepth();
    }
}

$box = new Box(10, 20, 30);
echo "Width: " . $box->width . "\n";
echo "Height: " . $box->getHeight() . "\n";

$bigBox = new BigBox(15, 25, 35);
echo "BigBox Depth: " . $bigBox->showDepth() . "\n";
OutputSuccess
Important Notes

Trying to access private or protected properties directly from outside the class will cause an error.

Protected members are useful when you want child classes to use or change something but keep it hidden from the outside.

Use public for things meant to be used freely, private for things to hide completely, and protected for things shared with child classes.

Summary

Access modifiers control who can see or change parts of your code.

Public means anyone can access, private means only inside the class, protected means inside the class and child classes.

They help keep your code safe and organized by hiding details you don't want changed directly.