Welcome to my series on every PHP keyword and its usage. Today’s items: private, protected and public.

These keywords are collectively known as visibility, and describe where a class constant, property or method can be accessed from.

  • A private constant, property or method can only be accessed from within the class that defines it.
  • A protected constant, property or method can only be accessed from within the class that defines it, or a descendant of that class.
  • A public constant, property or method can be accessed from anywhere.

By default, all constants, properties and methods are public unless specified otherwise.

As a general rule, it’s best to give everything the lowest visibility possible. It’s a lot easier to refactor when you know that the only code calling a method is in the current class.

Notes:

  • Visibility works the same way for static and instance properties/methods, and an instance of a class can call that class’ private constants, static properties and static methods.
  • Constants, properties and methods with private visibility cannot be overridden.
  • Visibility isolates calls between classes, not between instances. An object can call another object’s private method, provided that they are the same type of object.
  • Traits are not inherited so much as applied. This means that a private constant, property or method in the trait will be copied onto any object using that trait, and can be called directly from the object.

Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
class Example
{
	private const PRIVATE_CONSTANT = "private";
	protected const PROTECTED_CONSTANT = "protected";
	public const PUBLIC_CONSTANT = "public";
	
	private static $privateStaticProperty = "private";
	protected static $protectedStaticProperty = "protected";
	public static $publicStaticProperty = "public";
	
	private $privateProperty = "private";
	protected $protectedProperty = "protected";
	public $publicProperty = "public";
	
	private static function privateStaticMethod(){}
	protected static function protectedStaticMethod(){}
	public static function publicStaticMethod(){}
	
	private function privateMethod(){}
	protected function protectedMethod(){}
	public function publicMethod(){}
	
	public static function callPrivateStatic()
	{
		// This class has access to its own private constants, static
		// properties and static methods
		echo self::PRIVATE_CONSTANT;
		echo self::$privateStaticProperty;
		self::privateStaticMethod();
	}
	
	public static function callProtectedStatic()
	{
		// This class has access to its own protected constants, static
		// properties and static methods
		echo self::PROTECTED_CONSTANT;
		echo self::$protectedStaticProperty;
		self::protectedStaticMethod();
	}
	
	public static function callPublicStatic()
	{
		// This class has access to its own public constants, static properties
		// and static methods
		echo self::PUBLIC_CONSTANT;
		echo self::$publicStaticProperty;
		self::publicStaticMethod();
	}
	
	public function callPrivate()
	{
		// This class has access to its own private properties and methods
		echo $this->privateProperty;
		$this->privateMethod();
	}
	
	public function callProtected()
	{
		// This class has access to its own protected properties and methods
		echo $this->protectedProperty;
		$this->protectedMethod();
	}
	
	public function callPublic()
	{
		// This class has access to its own public properties and methods
		echo $this->publicProperty;
		$this->publicMethod();
	}
}

class ExampleChild extends Example
{
	public static function callProtectedStatic()
	{
		// This class has access to, and can override, its ancestors' protected
		// constants, static properties and methods
		echo parent::PROTECTED_CONSTANT;
		echo parent::$protectedStaticProperty;
		parent::protectedStaticMethod();
	}
	
	public static function callPublicStatic()
	{
		// This class has access to, and can override, its ancestors' public
		// constants, static properties and methods
		echo parent::PUBLIC_CONSTANT;
		echo parent::$publicStaticProperty;
		parent::publicStaticMethod();
	}
	
	public function callProtected()
	{
		// This class has access to, and can override, its ancestors' protected
		// properties and methods
		echo $this->protectedProperty;
		$this->protectedMethod();
	}
	
	public function callPublic()
	{
		// This class has access to, and can override, its ancestors' public
		// properties and methods
		echo $this->publicProperty;
		$this->publicMethod();
	}
}

class Other
{
	public static function callPublicStatic()
	{
		// This class has access to Example's public constants, static
		// properties and static methods
		echo Example::PUBLIC_CONSTANT;
		echo Example::$publicStaticProperty;
		Example::publicStaticMethod();
	}
	
	public function callPublic()
	{
		// This class has access to Example's public properties and methods
		$example = new Example();
		echo $example->publicProperty;
		$example->publicMethod();
	}
}

// The global scope also has access to Example's public constants, static
// properties, properties, static methods and methods.
echo Example::PUBLIC_CONSTANT;
echo Example::$publicStaticProperty;
Example::publicStaticMethod();
$example = new Example();
echo $example->publicProperty;
$example->publicMethod();