PHP access control (public protected private)

Source: Internet
Author: User
In PHP, the access control of the property or method is implemented in front of the public private protected and other keywords, public decorated class members can be accessed anywhere, private decorated class members can only be accessed by themselves, Protected decorated class members can be accessed by themselves or by their subclasses.


     classtest{ Public$public=' Public ';Private$private=' Private ';protected$protected=' protected '; Public functionshow(){Echo$this-Private; } Public functionshow2(){Echo$this-protected; }    }$test=NewTest ();Echo$test- Public;$test->show ();$test->show2 ();//exit ();//Echo $test->protected; will produce a fatal error//Echo $test->private; It also produces a fatal error. The private property can only be accessed in defined classes/**    *     */ classtest2extendstest {//Override the show () method of the parent class functionshow(){//inherit from protected of parent classEcho$this-protected; }    }$test 2=NewTest2 ();Echo"
      ";//$test 2->protected;//will produce a fatal error protected property cannot be accessed externally$test 2->show ();//access to protected can only be accessed by itself or its subclasses?>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP access control (public protected private), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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