Differences between public, private, and protected in phpclass and instance analysis-PHP Tutorial

Source: Internet
Author: User
Differences between public, private, and protected in phpclass and instance analysis. I. differences between public, private, and protected: the permissions are the largest, and can be called internally or by instances. Protected: protected type, used for calls of this class and inheritance class. Private: private I. differences between public, private, and protected
Public: The permission is the largest, and can be called internally or by instances.
Protected: protected type, used for calls of this class and inheritance class.
Private: private type, which is only used in this class.

II. instances

The code is as follows:


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, which can be called internally
Echo $ this-> protected; // protected, which can be called internally
$ This-> pri_function (); // private method, which can be called internally
$ This-> pro_function (); // 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: pro_function () from context
$ Test-> pri_function (); // Fatal error: Call to private method test: pri_function () from context
?>


As shown in the preceding example,
Public: it can be called inside the class and instantiated.
Private: class can be called internally. if it is instantiated, an error is returned.
Protected: it can be called internally by class. if it is instantiated, an error is returned.

The code is as follows:


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 be inherited, changed to 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: pri_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 (); // The subclass and its parent class have attributes and methods of the same name. When instantiating the subclass, the attributes and methods in the subclass will overwrite the parent class.
?>


As shown in the preceding example,
Public: The public in test can be inherited.
Private: private in test cannot be inherited.
Protected: protected in test can be inherited.
Static: static in test can be inherited.
Alas, I always don't like to remember these things. when I use them, I always feel wrong and want to check them again. so I will write an example for you to view it easily.

Permission public: the permission is the largest and can be called internally or by instances. Protected: protected type, used for calls of this class and inheritance class. Private: private...

Related Article

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.