PHP 7 interview question · Most asked

Explain `public`, `protected` and `private` visibility in PHP.

Summary

public members are accessible everywhere, protected members within the class and its subclasses, and private members only within the declaring class.

Longer Answer

Visibility in PHP is class-based, not object-based: an object can access private members of another instance of the same class (useful in equals() methods). Prefer the narrowest visibility; private + final classes reduce the API surface you must maintain.

Key Points

  • Public → anywhere
  • Protected → class hierarchy
  • Private → declaring class only
  • Class-based, not instance-based

More most-asked PHP 7 questions

← All PHP 7 questions