Public, protected, private
Used to describe:
Member, which locations in the source code are accessible.
Public: In the class, outside the class, within the inheritance chain class
Protected: Within the class, within the inheritance chain class.
Private: Within this category.
One: Private: within this category.
<?php
Class H {
Public Function Hgetp () {
Echo $this->p;
echo self:: $sp;
// }
public static function Hgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class Z extends H {
protected $p = ' value ';
Private $p = ' value ';//$p is private and can only be called within this class
public static $sp = ' Static-value ';
Public Function Zgetp () {
echo self:: $sp;
// }
public static function Zgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class K extends Z {
Public Function Kgetp () {
Echo $this->p;
echo self:: $sp;
}
public static function Kgetp () {
Echo $this->p;
echo self:: $sp;
//}
}
Echo H::hgetp ();
Echo Z::zgetp ();
Echo K::kgetp ();
$o = new K;
$o->KGETP ();//output: notice:undefined property:k:: $p
echo "Var_dump ($o);//Output: Object (K) #1 (1) {["P": "Z":p rivate]=> string (5) "Value"}
$o->zgetp (); Output: Static-value
Two Protected: Within the class, within the inheritance chain class. (can be called within the inherited system of a class)
<?php
Class H {
Public Function Hgetp () {
Echo $this->p;
echo self:: $sp;
// }
public static function Hgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class Z extends H {
protected $p = ' value ';//$p is protected in this class, which is called inside the inheritance system of this class.
Private $p = ' value '; $p is private and can only be called inside this class
public static $sp = ' Static-value ';
Public Function Zgetp () {
echo self:: $sp;
// }
public static function Zgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class K extends Z {
Public Function Kgetp () {
Echo $this->p;
echo self:: $sp;
}
public static function Kgetp () {
Echo $this->p;
echo self:: $sp;
//}
}
Echo H::hgetp ();//If it is a static member, the parent class cannot access the static members defined by the child class.
Echo Z::zgetp ();
Echo K::kgetp ();
$o = new K;
$o->KGETP ();//output: Value
echo "Var_dump ($o);//Output: Object (K) #1 (1) {["P": "Z":p rivate]=> string (5) "Value"}
$o->zgetp (); Output: Static-value
PHP Inheritance:
Private members can be inherited.
If it is a static member, the parent class cannot access the static members defined by the child class.
Access modifiers for PHP classes