Difference between public, protected, and private: protectedprivate

Source: Internet
Author: User

Difference between public, protected, and private: protectedprivate

Use className: functionNmae (), instantiation, parent, and self to draw a conclusion.

ClassName: functionNmae () must be a static method (public Mode)

Parent: functionNmae () the parent class must be a static method (public or protected)

Self: functionNmae () This class must be a static method (three modes)

Instantiation: after instantiation, you can access static or common methods in public mode.

 

1. Direct Access (via className: functionNmae () and instantiation ):

Conclusion:

| It can be accessed through className: functionNmae ()
Public
| It can be accessed through instance $ obj-> functionName ()

| Cannot be accessed through className: functionNmae ()
Protected
| Cannot be accessed through instance $ obj-> functionName ()

| Cannot be accessed through className: functionNmae ()
Private
| Cannot be accessed through instance $ obj-> functionName ()

<? Php/*** @ authors HG (hg0728@qq.com) * @ date 17:12:02 * @ version 1.0 */header ("Content-type: text/html; charset = UTF-8 "); class A {static $ name = 'This is the static variable of class A'; static public function stc_pc () {echo 'This is the public static method of class A <br/> ';} static function stc () {echo 'This is A static method of Class A <br/>';} static protected function stc_pd () {echo 'This is A protected static method of Class A <br/>';} static private function stc_pe () {echo 'This is A private static method of Class A <br/>';} A: stc_pc (); // you can access A: stc (); // accessible A: $ name; // No error/A: stc_pd (); // inaccessible // A: stc_pe (); // inaccessible echo '<br> access through instantiation <br>'; $ a = new A (); $ a-> stc_pc (); // accessible $ a-> stc (); // accessible // $ a-> $ name; // error // $ a-> stc_pd (); // inaccessible // $ a-> stc_pe (); // inaccessible/********************* | you can use className: functionNmae () access public | access through instance $ obj-> functionName () | protected cannot be accessed through className: functionNmae () | access through instance $ obj-> functionName () access | private cannot be accessed through className: functionNmae () | the instance $ obj-> functionName () cannot be used () access ********************/

2. Direct Access (self: functionNmae ())

Conclusion:

Self: functionNmae (); must be a static method (either of the three modes)

<? Php/*** @ authors HG (hg0728@qq.com) * @ date 17:18:50 * @ version 1.0 */header ("Content-type: text/html; charset = UTF-8 "); class C {static $ name = "static variable"; static public function stc_pc () {echo 'static public method <br> '; self: stc_pd (); self :: stc_pe (); self: stc (); // self: func (); // error reported but result} static protected function stc_pd () {echo 'static protected method <br> ';} static private function stc_pe () {echo 'static protected method <br>';} static function stc () {echo 'static normal method <br> ';} function func () {echo 'normal method <br>';} C: stc_pc (); /***** self: functionNmae (); it must be a static method (either in three modes )******/

 

3. Access the parent class through subclass (parent: functionNmae () and instantiate)

Conclusion:

Parent: functionNmae (); private cannot be accessed
$ Obj-> functionNmae (); private cannot be accessed

<? Php/*** @ authors HG (hg0728@qq.com) * @ date 17:18:50 * @ version 1.0 */header ("Content-type: text/html; charset = UTF-8 "); include '/class_a.php'; class B extends A {static public function index () {parent: stc_pc (); parent: stc (); parent: stc_pd (); parent: $ name; // No error // parent: stc_pe (); // inaccessible echo '<br> access through instantiation <br>'; $ a = new A (); $ a-> stc_pc (); $ a-> stc (); $ a-> stc_pd (); $ a-> $ name; // error // $ a-> stc_pe (); // not allowed} B: index ();/****** In the subclass, use: parent: functionNmae (); private: $ obj-> functionNmae (); private ******/

 

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.