Differences between public, protected, and private

Source: Internet
Author: User
: This article mainly introduces the differences between public, protected, and private. For more information about PHP tutorials, see. 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 ()

 ';} Staticfunction stc () {echo' this is A static method of Class.
';} Staticprotectedfunction stc_pd () {echo' this is A protected static method of Class.
';} Staticprivatefunction stc_pe () {echo' this is A private static method of Class.
';}} A: stc_pc (); // you can access A: stc (); // you can access A: $ name; // no error is reported. //:: stc_pd (); // inaccessible // A: stc_pe (); // inaccessible echo'

Access through instantiation

'; $ A = new A (); $ a-> stc_pc (); // you can access $ 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)

 '; Self: stc_pd (); self: stc_pe (); self: stc (); // self: func (); // error reported but result returned} staticprotectedfunction stc_pd () {echo 'static protected method
';} Staticprivatefunction stc_pe () {echo' static protected method
';} Staticfunction stc () {echo' static normal method
';} Function func () {echo' normal method
';}} C: stc_pc ();/****** self: functionNmae (); static method required (available in all 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

 
Access through instantiation

'; $ A = new A (); $ a-> stc_pc (); $ a-> stc (); $ a-> stc_pd (); $ a-> $ name; // error // $ a-> stc_pe (); // no} B: index (); /***** in the subclass: parent: functionNmae (); private: $ obj-> functionNmae (); private: inaccessible ******/

The differences among public, protected, and private are described above, including some content. I hope to help some friends who are interested in PHP tutorials.

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.