The differences between public,private,protected in PHP class, and examples
One, the difference between the public,private,protected
Public: Permissions are maximum, can be called internally, instance calls, and so on.
Protected: Protected type, used for this class and for inheriting class calls.
Private: Proprietary type, only used in this class.
Two, examples
public = ' Public '
'; $this->private = ' private
'; $this->protected = ' protected
'; } static function tank () {if (!isset (self:: $instance [Get_class ()])) {$c = Get_class (), self:: $instance = new $c;} return Self:: $instance; } public Function Pub_function () {echo ' request public function
"; Echo $this->public; Echo $this->private; Private, Echo $this->protected; can be called internally Protected, internal can call $this->pri_function (); Private method, internal can call $this->pro_function (); Protected method, internal can be called} protected function pro_function () {echo "You request protected function
"; } Private Function Pri_function () {echo "You request private function
"; }} $test = Test::tank (); Echo $test->public;echo $test->private; Fatal Error:cannot Access private property test:: $privateecho $test->protected; Fatal error:cannot Access protected property test:: $protected $test->pub_function (); $test->pro_function (); Fatal Error:call to protected method test::p ro_function () from context$test->pri_function (); Fatal Error:call to Private method test::p ri_function () from context?>
From the above example, we can see that
Public:??? can be called within class, and can be instantiated.
Private:?? Can be called within the class, the instantiation of the call error.
Protected:? Can be called within the class, the instantiation of the call error.
public = ' Public '
'; $this->private = ' private
'; $this->protected = ' protected
'; } protected function tank () {//Private method cannot inherit, replace with public,protected if (self:: $instance [Get_class () ]) {$c = Get_class (); self:: $instance = new $c;} Return to self:: $instance; } public Function Pub_function () {echo ' request public function
"; Echo $this->public; } protected function Pro_function () {echo "You request protected function
"; Echo $this->protected; } Private Function Pri_function () {echo "You request private function
"; Echo $this->private; }}class Test1 extends test{public function __construct () {Parent::tank (); Parent::__construct ();} public function tank () {echo $this->public; echo $this->private; notice:undefined property:test1:: $private echo $this->protected; $this->pub_function (); $this->pro_function (); $this->pri_function (); Fatal Error:call to Private method test::p ri_function () from the context ' test1 '} public Function pro_extends_function () { echo "You request extends_protected function
"; } Public Function pri_extends_function () {echo ' You request Extends_private function
"; }} Error_reporting (e_all), $test = new Test1 (), $test-tank (); Subclasses and parent classes have properties and methods of the same name, and when instantiating subclasses, the properties and methods in the subclass cover the parent class.?>
? From the above example, we can see that
Public:??? The public in test can be inherited.
Private:?? the private in test cannot be inherited.
Protected:? The protected in test can be inherited.
Static:??????? The static in test can be inherited.
Alas, for these things, always do not like to remember, when used, always feel wrong, but also to check, so write an example, convenient to view.