The differences between public,private,protected in PHP class, and examples

Source: Internet
Author: User
Tags php class

One, public,private,protected the difference public: permissions are the largest, 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, instance view copy print?            <?php error_reporting (E_all);       Class test{public $public;       Private $private;       protected $protected;       Static $instance;       Public Function __construct () {$this->public = ' public <br> ';       $this->private = ' private <br> ';       $this->protected = ' protected <br> ';       } static function tank () {if (!isset (self:: $instance [Get_class ()])) {$c = Get_class ();       Self:: $instance = new $c;       } return Self:: $instance;       } public Function Pub_function () {echo "Your request public function<br>";       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<br&gt       ;";       }       Private Function Pri_function () {echo "You request private function<br>";      }} $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 the context?> from the above example, we can see that public: can Clas s internal invocation, which can be instantiated. Private: Can be called within the class, the instantiation of the call error. Protected: Can call inside class, instantiation call error.    View copy print?       <?php class test{public $public;       Private $private;       protected $protected; StatIC $instance;       Public Function __construct () {$this->public = ' public <br> ';       $this->private = ' private <br> ';       $this->protected = ' protected <br> '; } protected function tank () {//Private method cannot inherit, replace with public,protected if (self:: $instance       [Get_class ()]))       {$c = Get_class ();       Self:: $instance = new $c;       } return Self:: $instance;       } public Function Pub_function () {echo "Your request public function<br>";       Echo $this->public;       } protected function Pro_function () {echo "You request protected function<br>";       Echo $this->protected;       } Private Function Pri_function () {echo "You request private function<br>";       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_e       Xtends_function () {echo "You request extends_protected function<br>";       } public Function Pri_extends_function () {echo ' You request extends_private function<br> ';      }} 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 example above, we can see that the public in public:test can be inherited. Private in the private:test can not be inherited. The protected in protected:test can be inherited. Static in the static: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.

The differences between public,private,protected in PHP class, and examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.