The difference between public, protected, private, protectedprivate_php tutorial

Source: Internet
Author: User

The difference between public, protected and private, protectedprivate


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 (hg0728@qq.com) * @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 (hg0728@qq.com) * @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 (hg0728@qq.com) * @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 * * * **/

http://www.bkjia.com/PHPjc/1006439.html www.bkjia.com true http://www.bkjia.com/PHPjc/1006439.html techarticle the difference between public, protected, private, protectedprivate through Classname::functionnmae (), instantiation, parent, self to conclude className:: Functionnmae () requires a static method (public mode ...) .

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