PHP Object-oriented (OOP programming)-Class access modifiers

Source: Internet
Author: User
Tags class access modifiers modifiers
The access modifiers of the type allow developers to restrict access to class members, which is a new feature of PHP5


Private
Protected Public
In the same class
Class in the subclass of the
All the external members

 
  Public;echo $this->protected;echo $this->private;}} $obj = new MyClass (), Echo $obj->public;//worksecho $obj->protected;//Fatal errorecho $obj->private;//Fatal Er Ror$obj->printhello ();//shows public, Protected and private/** * Define MyClass2 */class MyClass2 extends myclass{//W E can redeclare the public and protected method, but not privateprotected $protected = ' Protected2 '; function Printhello () { echo $this->public;echo $this->protected;echo $this->private;}} $obj 2 = new MyClass2 (), Echo $obj->public;//worksecho $obj 2->private;//undefinedecho $obj 2->protected;// Fatal Error$obj2->printhello ();//shows public, Protected2, not private?>



!--? php/** * Class method access control * Define MyClass */class myclass{//contructors must be publicpublic function _ _construct () {}//Declare a public methodpublic function Mypublic () {}//Declare a protected methodprotected function My Protected () {}//abstract Protected function Funprotect ($param); Abstract methods can only be defined in abstract classes//Declare a private methodprivate fun Ction myprivate () {}//This is Publicfunction Foo () {$this--->mypublic (); $this->myprotected (); $this->myprivate ( );}} $myclass = new MyClass; $myclass->mypublic ();//works$myclass->myprotected ();//Fatal error$myclass-> Myprivate ();//Fatal Error$myclass->foo ();//Public, Protected and Private work/** * Define MyClass2 */class MyClass2 E Xtends myclass{//This is Publicfunction Foo2 () {$this->mypublic (); $this->myprotected (); $this->myprivate () ;//Fatal Error}} $myclass 2 = new MyClass2; $myclass 2->mypublic ();//Works$myclass2->foo2 ();//Public and Protected work, not private?> 
  • 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.