"Getting Started with PHP object-oriented (OOP) programming" 13. Type of Access (public,protected,private)

Source: Internet
Author: User
Tags getting started with php modifiers

The access modifiers of the type allow developers to restrict access to class members , which is a new feature of PHP5, but is a good feature of the OOP language. And most OOP languages already support this feature. PHP5 supports the following 3 types of access modifiers:

Public (common, default), protected (protected) and private (private) three kinds.

Public, members of the class will not have access restrictions, all external members can access (read and write) This class member (including member properties and member methods), in all versions prior to PHP5, the members of the class in PHP are public, And in PHP5, if a member of the class does not specify a member access modifier, it is considered public. Cases:

Public $name;p ublic function Say () {};

Private proprietary modifier, defined as a member of private, is visible to all members of the same class, that is, there is no access restriction, but the external code for the class is not allowed to change or even read operations, and for subclasses of that class, members of the private adornment cannot be accessed. Cases:

Private $var 1 = ' A ';//attribute Private function GetValue () {}//function
class internal access is: $this->var1, $this->getvalue ()

Protected protected member modifiers, members that are decorated as protected cannot be accessed by the class's external code. However, for subclasses of this class, you have access to properties, methods, and read and write operations, and the subclass's external code, including its subclasses, does not have permission to access its properties and methods.

Cases:

Protected $name;p rotected function Say () {};
Private Protected Public
In the same class
Class in the subclass of the
All the external members

Property access Control Examples:

<?php/** * Define MyClass */class myclass{public $public = ' public ';p rotected $protected = ' protected ';p rivate $private = ' Private '; function Printhello () {echo $this->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?>

Method Access Control Example:

<?php/** * Define MyClass */class myclass{//contructors must be publicpublic function __const Ruct () {}//Declare a public methodpublic function Mypublic () {}//Declare a protected methodprotected function Myprotec Ted () {}//Declare a private Methodprivate function 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?> 

It is also important to note that when subclasses override the methods of the parent class, the access rights of the methods in the subclass must not be lower than the access rights of the parent class's overridden methods, that is, the access to the parent class method must be higher or equal.

For example, if the access permission for a parent method is protected, then the permissions to be overridden in the subclass are protected and public, and if the method of the parent class is public, the method to be overridden in the subclass is only public, In summary, the methods in the subclass are always higher or equal to the access rights of the parent class's overridden methods.

"Getting Started with PHP object-oriented (OOP) programming" 13. Type of Access (public,protected,private)

Related Article

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.