Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP visibility: public, protected, private

Reference for PHP visibility modifiers: public, protected and private for properties, methods and constants, how access works in subclasses, and common errors.

Visibility controls where a property, method or class constant can be accessed from. PHP has three levels:

  • public: accessible from anywhere.
  • protected: accessible inside the class and its subclasses (and parent classes of the declaring class).
  • private: accessible only inside the class that declares it.

The check is done at run time against the calling scope. Methods and constants without a modifier are public; properties must have a modifier (or var).

PHP
class Account
{
    private float $balance = 0;
    protected string $owner = "n/a";
    public function deposit(float $amount): void
    {
        $this->balance += $amount;
    }
}

Try it

PHP What each level allows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Private methods and sibling access

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
public    function name() {}
protected function name() {}
private   function name() {}

public    int $property;
private   const SECRET = 1;     // visibility on constants: PHP 7.1+
Level Same class Subclass Outside
public yes yes yes
protected yes yes no
private yes no no

Errors: breaking a rule throws Error ("Cannot access private property ..." or "Call to private method ..."). A subclass cannot make a member less visible than the parent; it can widen protected to public.

Usage notes

  • Visibility is per class, not per object. Code in a class can read private members of any other instance of that class, as plus() does above.
  • A subclass cannot see its parent's private properties. Reading one from a child method gives an "Undefined property" warning, not an error, because the child simply has no such property.
  • Private members are not overridden. A subclass that declares the same name creates a separate member; the parent keeps using its own.
  • Private constructor prevents new from outside. It is the basis of singletons and named constructors (Money::fromCents()).
  • Prefer the narrowest level that works. Start with private, widen only when a subclass needs access.
  • Magic methods like __get() are called when code tries to read an inaccessible or missing property.
  • Reflection can bypass visibility (since PHP 8.1 setAccessible() is no longer needed). Do not rely on privacy for security.
  • final stops a method or constant from being overridden; it is separate from visibility. Private methods cannot be final, except constructors.
  • Asymmetric visibility (PHP 8.4) lets a property be read publicly but written privately: see Asymmetric visibility.