Php object-oriented full strategy (9) access types

Source: Internet
Author: User

13. Access type
Type access modifiers allow developers to restrict access to class members. This is a new feature of PHP5,
It is a good feature of OOP. In addition, most OOP languages support this feature. PHP5 supports the following three access methods:
Question Modifier
Public (public, default), private (private), and protected (protected.
Public modifier. No access restriction is imposed on members in the class, and all external members can access (read and write)
This class member (including member attributes and member methods), in all versions prior to PHP5, PHP class members are
Public, and in PHP5, if the class member does not specify a member access modifier, it will be treated as public.
Example: public $ name;
Public function say (){};
Private modifier, which is defined as a private member. it is visible to all members in the same class, that is
There is no access restriction. However, external code of this class cannot be changed or even read.
Members that can access private modification.
Example: private $ var1 = 'a'; // attribute
Private function getValue () {}// function
Protected protects the member modifier. The member modified as protected cannot be accessed by the class's external code. However
Sub-classes of this class have access permissions and can perform read and write operations on attributes and methods. The external code of this sub-class includes its
Subclass does not have the permission to access its attributes and methods.
Example: protected $ name;
Protected function say (){};
Private protected public
√ In the same class
√ In the subclass of the class
All external members √
Code snippet
Copy codeThe Code is as follows:
<? Php
/**
* Define MyClass
*/
Class MyClass {
Public $ public = 'public ';
Protected $ protected = 'protected ';
Private $ private = 'private ';
Function printHello (){
Echo $ this-> public;
Echo $ this-> protected;
Echo $ this-> private;
}
}
$ Obj = new MyClass ();
Echo $ obj-> public; // Works
Echo $ obj-> protected; // Fatal Error
Echo $ obj-> private; // Fatal Error
$ Obj-> printHello (); // Shows Public, Protected and Private
/**
* Define MyClass2
*/
Class MyClass2 extends MyClass {
// We can redeclare the public and protected method, but not private
Protected $ protected = 'protected2 ';
Function printHello (){
Echo $ this-> public;
Echo $ this-> protected;
Echo $ this-> private;
}
}
$ Obj2 = new MyClass2 ();
Echo $ obj-> public; // Works
Echo $ obj2-> private; // Undefined
Echo $ obj2-> protected; // Fatal Error
$ Obj2-> printHello (); // Shows Public, Protected2, not Private
?>

Code snippet
Copy codeThe Code is as follows:
<? Php
/**
* Define MyClass
*/
Class MyClass {
// Contructors must be public
Public function _ construct (){}
// Declare a public method
Public function MyPublic (){}
// Declare a protected method
Protected function MyProtected (){}
// Declare a private method
Private function MyPrivate (){}
// This is public
Function 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 extends MyClass {
// This is public
Function Foo2 (){
$ This-> MyPublic ();
$ This-> MyProtected ();
$ This-> MyPrivate (); // Fatal Error
}
}
$ Myclass2 = new MyClass2;
$ Myclass2-> MyPublic (); // Works
$ Myclass2-> Foo2 (); // Public and Protected work, not Private
?>

In addition, when the subclass overrides the parent class method, you must note that the method access permission in the subclass must not be lower than that of the parent class.
The access permission of the override method, that is, it must be higher than or equal to the access permission of the parent method.
For example, if the access permission of the parent class method is protected, the permission to be overwritten in the subclass must be protected.
And public. If the method of the parent class is public, the method to be overwritten in the subclass can only be public.
The method is always higher than or equal to the access permission of the parent class to be overwritten by the method.

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.