: 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.