Current Location: Home> Latest Articles> Understanding PHP Access Modifiers: public, protected, private, final, and abstract Explained

Understanding PHP Access Modifiers: public, protected, private, final, and abstract Explained

M66 2025-07-09

Overview of PHP Access Modifiers

In PHP development, access control is a core mechanism for ensuring code security and modular design. By using access modifiers appropriately, developers can restrict the visibility of class members, avoid misuse, and enhance maintainability.

PHP provides five main access modifiers: public, protected, private, final, and abstract. Let's explore each one with examples to understand how they work in practice.

public Modifier

The public modifier declares a member as accessible from anywhere—inside the class, outside the class, or from any subclass. It's the default access level and is suitable for properties or methods that need to be widely accessible.

class Person {
    public $name;

    public function sayHello() {
        echo 'Hello, my name is ' . $this->name;
    }
}

$person = new Person();
$person->name = 'Tom';
$person->sayHello();

protected Modifier

The protected modifier allows access only within the class itself and its subclasses. It cannot be accessed directly from outside the class hierarchy.

class Animal {
    protected $name;

    protected function getName() {
        return $this->name;
    }
}

class Cat extends Animal {
    public function setName($name) {
        $this->name = $name;
    }

    public function sayHello() {
        echo 'Hello, I am ' . $this->getName();
    }
}

$cat = new Cat();
$cat->setName('Tom');
$cat->sayHello();

private Modifier

The private modifier restricts access to members strictly within the class itself. Subclasses and external code cannot access these members.

class Person {
    private $name;

    private function getName() {
        return $this->name;
    }

    public function sayHello() {
        echo 'Hello, my name is ' . $this->getName();
    }
}

$person = new Person();
$person->name = 'Tom';  // This will trigger an error
$person->sayHello();

final Modifier

The final modifier prevents a class from being extended or a method from being overridden. It is used to lock down critical functionality that should not be altered.

class Animal {
    final public function eat() {
        echo 'I am eating';
    }
}

class Cat extends Animal {
    public function eat() {  // This will trigger an error
        echo 'I am not eating';
    }
}

$cat = new Cat();
$cat->eat();

abstract Modifier

The abstract modifier is used to declare abstract classes and methods. Abstract classes cannot be instantiated directly and must be extended. Abstract methods provide a blueprint that must be implemented in child classes.

abstract class Animal {
    abstract public function eat();
}

class Cat extends Animal {
    public function eat() {
        echo 'I am eating fish';
    }
}

$cat = new Cat();
$cat->eat();

Conclusion

Understanding and correctly using PHP access modifiers is essential for writing robust, secure, and scalable code. Each modifier serves a distinct purpose, and developers should choose the appropriate visibility level based on actual requirements to improve code encapsulation and maintain stability.