The difference between public, protected and private
Draw conclusions from Classname::functionnmae (), instantiation, parent, self
Classname::functionnmae () requires a static method (public mode)
Parent::functionnmae () parent class needs to be static method (public, protected two modes)
Self::functionnmae () This class needs to be a static method (three modes)
Instantiation: A static or generic method that can access public mode after instantiation
I. Direct access (via Classname::functionnmae () and instantiation):
Conclusion:
| can be accessed via Classname::functionnmae ()
Public
| can be accessed by instance $obj->functionname ()
| Not accessible via Classname::functionnmae ()
Protected
| Cannot access by instance $obj->functionname ()
| Not accessible via Classname::functionnmae ()
Private
| Cannot access by instance $obj->functionname ()
Php/** * * @authors HG ([email protected]) * @date 2015-05-26 17:12:02 * @version 1.0*/Header("Content-type:text/html;charset=utf-8");classA {Static $name= ' This is a static variable of Class A '; Static Public functionstc_pc () {Echo' This is a public static method of Class A
'; } Static functionSTC () {Echo' This is a static method of Class A
'; } Static protected functionstc_pd () {Echo' This is a static method of Class A protected
'; } Static Private functionStc_pe () {Echo' This is a private static method of Class A
'; }}a:: Stc_pc ();//access toA:: STC ();//access toA::$name;//Do not error//a::stc_pd ();//unreachable//a::stc_pe ();// inaccessibleEcho'
By instantiating access
';$a=NewA ();$a->STC_PC ();//access to$a->STC ();//Accessible //$a $name;//error//$a->stc_pd ();//unreachable//$a->stc_pe () //inaccessible/******************** | You can access public by Classname::functionnmae () | | can be accessed by instance $obj->functionname () | Cannot access protected by Classname::functionnmae () | | Cannot access by instance $obj->functionname () | Cannot access private through Classname::functionnmae () | | Cannot access ******************* by instance $obj->functionname ()*/
Ii. Direct Access (Self::functionnmae ())
Conclusion:
Self::functionnmae (): Static method required (three modes available)
Php/** * * @authors HG ([email protected]) * @date 2015-05-26 17:18:50 * @version 1.0*/Header("Content-type:text/html;charset=utf-8");classC {Static $name= "Static Variable"; Static Public functionstc_pc () {Echo' Static public method
'; Self::stc_pd (); Self::Stc_pe (); Self::STC (); //Self::func ();//Error but with results } Static protected functionstc_pd () {Echo' Static protected method
'; } Static Private functionStc_pe () {Echo' Static protected method
'; } Static functionSTC () {Echo' Static Common method
'; } functionfunc () {Echo' Common method
'; }}c::stc_pc ();/******self::functionnmae (); requires a static method (three modes are available) * * * **/
Iii. accessing the parent class through subclasses (Parent::functionnmae () and instantiation)
Conclusion:
Parent::functionnmae (); Private not accessible
$obj->functionnmae (); Private not accessible
Php/** * * @authors HG ([email protected]) * @date 2015-05-26 17:18:50 * @version 1.0*/Header("Content-type:text/html;charset=utf-8");include'/class_a.php ';classBextendsa{Static Public functionIndex () {Parent::stc_pc (); Parent::STC (); Parent::stc_pd (); Parent::$name;//no error//parent::stc_pe ();//Unreachable Echo'
By instantiating access
'; $a=NewA (); $a-stc_pc (); $a-STC (); $a-stc_pd (); $a-$name;//error//$a->stc_pe (); //No}}b::index ();/** * * * * passed in subclass: Parent::functionnmae (); private inaccessible $obj->functionnmae (); Private inaccessible * * * **/
Four
Php/** * * @authors HG ([email protected]) * @date 2015-05-28 11:54:03 * @version 1.0*/classfunctioninc{functionfc_a () {Echo' 000 '; } Static protected functionFc_b () {Echo' 111 '; } }classFunextendsFunctioninc {Static functionA () {parent:: Fc_a ();//error fc_a () is not a static methodParent::Fc_b (); } functionB () {Parent::fc_a (); Parent::Fc_b (); }}/*a static method in a subclass invokes the parent class method through parent, and the parent class method needs to be a static method. The normal method calls the parent-class method through parent without restriction. */$a=NewFun ();$a-a ();$a->b ();