Visibility¶
Properties, methods and classes may have a visibility. It limits the scope of the definition:
private: to the current classprotected: to the current class hierarchy, parents and children classespublic: to all the codevar: to all the code, deprecated
The default visibility is public. The visibility may be omitted when using another option, such as final, static. It is recommended to always specify the visibility explicitly.
Visibility may no change, unless when overwriting it with a trait, or in a child class.
Visibility may be by-passed with class-invasion, where an object of one class may access another object’s property, as long as they are from the same class or family.
<?php
class X {
public const X = 1;
private $property;
protected function foo() {}
}
?>
See also Understanding The Concept Of Visibility In Object Oriented PHP and PHP Tricks: Access control bypass.
Related : Properties, Method, Static Constant, Class Constants Visibility, Class Invasion, Object, Private Visibility, Protected Visibility, Public Visibility, Var, Asymmetric Visibility, Constructor, Readable, Writable, get_object_vars(), Class Getter Method, Variable Scope, Writeable, Permission