Error_reporting (E_all); Class test{ Public $public; Private $private; protected $protected; Static $instance; Public Function __construct () { $this->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 "You request public function "; Echo $this->public; Echo $this->private; Private, internal can be called Echo $this->protected; Protected, internal can be called $this->pri_function (); Private method, which can be called internally $this->pro_function (); The protected method, which can be called internally } 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:: $private Echo $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 ?> 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:: $private echo $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 inside class, you can instantiate the call. Private: Can be called within the class, the instantiation of the call error. Protected: Can call inside class, instantiation call error. Class test{ Public $public; Private $private; protected $protected; Static $instance; Public Function __construct () { $this->public = ' public '; $this->private = ' private '; $this->protected = ' protected '; } protected function tank () {//Private method cannot inherit, replace with public,protected if (!isset (self:: $instance [Get_class ()])) { $c = Get_class (); Self:: $instance = new $c; } Return self:: $instance; } Public Function pub_function () { echo "You 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 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. ?> |