Php object-oriented access control public, private, and protected

Source: Internet
Author: User

The article uses examples to illustrate the details of object-oriented access control Public, private, and protected in PHP5. If you need them, please refer to them.

In PHP5, the object-oriented mechanism is enhanced, and access control mechanisms such as public, private, and protected are added. Literally:

Public is undoubtedly the Public meaning, meaning that the class itself and its external subclass can access this attribute or method;

Private: only the class can be accessed within the class itself-the instantiated object handle cannot access this attribute and method, and the subclass cannot access it;

The Protected attribute or method of Protected. This attribute or method can only be accessed by the class itself or the subclass. The instantiated object handle cannot be accessed.

Here is an example of this problem.

Assume that there is Xiao Li, the administrator of the GEM Museum, who divides the gems in the warehouse into three categories: Ruby, sapphire, and emerald. In addition, the property is divided. Anyone in the country can access the Ruby with the label (public), and the sapphire administrator's little Li family (protected ), the green gem was seen by Xiao Li on the hill (private ).

We can think like this:

The ruby of the public tag is owned by the whole people of the country, so long as citizens of the legal country can refer to and take photos.

Protected: This is Protected. Only the Xiao Li family members (Xiao Li's child or grandson) can visit and take photos in a specific room. They are not allowed to take photos outside the room. It's too stingy!

Private: This is Private. Xiao Li does not want to let others know, but does not want to take photos. Therefore, he can only look at Xiao Li's room, other people cannot even see Xiao Li's children.


Instance

The Code is as follows: Copy code

<? 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 "you request public function <br> ";

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 <br> ";

}

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: pro_function () from context

$ Test-> pri_function (); // Fatal error: Call to private method test: pri_function () from context

 

?>

 

 

<? 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 "you request public function <br>"; echo $ this-> public; echo $ this-> private; // private, echo $ this-> protected; // protected can be called internally, and $ this-> pri_function (); // private method can be called internally, you can call the $ this-> pro_function (); // protected method internally. You can call} protected function pro_functi internally. On () {echo "you request protected function <br>" ;}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: 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.

<? 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 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 <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: pri_function () from context 'test1'

}

 

Public function pro_extends_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 (); // 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.

 

?>

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.