PHP object-oriented Learning (full understanding of abstraction, encapsulation, inheritance, and polymorphism)

Source: Internet
Author: User
Tags modifiers

The three main characteristics of the aspect direction: encapsulation, inheritance, and Polymorphism:
When we first define a class, we actually extract the common attributes and behaviors of a class of things to form a physical model (Template). This method of research is called abstraction.

I. Encapsulation
Encapsulation is to encapsulate the extracted data and the operations on the data, and the data is protected internally. Only authorized operations (methods) in other parts of the program can perform operations on the data.
Php provides three access control Modifiers
Public indicates global. This class can be accessed inside, outside, and subclass.
Protected indicates that it is protected and can only be accessed by this class or subclass.
Private indicates private, which can only be accessed within this class.
The above three modifiers can be used to modify both the method and the attribute (variable). If there is no access modifier for the method, the default value is public, and the access modifier must be specified for the member attribute, in PHP4, var $ name indicates public attributes, which is not recommended.
Example: Copy codeThe Code is as follows: <? Php
Class Person {
Public $ name;
Protected $ age;
Private $ salary;
Function _ construct ($ name, $ age, $ salary ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> salary = $ salary;
}
Public function showinfo (){
// This indicates that all three modifiers can be used inside the class.
Echo $ this-> name. "|". $ this-> age. "|". $ this-> salary;
}
}
$ P1 = new Person ('zhang san', 20,3000 );
// This is an external class. If you use the following method to access age and salary, an error is returned.
// Echo $ p1-> age; echo $ p1-> salary;
?>

What should I do if I want to access the protected and private elements and methods externally? The common practice is to access the variable formats through the public function:
Public function setxxxx ($ val ){
$ This-> xxxx = $ val;
}
Public function getxxxx (){
Return $ this-> xxxx;
}
Set and get are provided here for convenience of identification and are not necessary.
For example:
Public function getsalary (){
Return $ this-> salary; // Extension: Some methods can be called here, such as determining the user name.
}
Echo $ p1-> getsalary ();
If you want to access protected and private, you can also use the following method, but it is not recommended to use it, as long as you know.
_ Set () and _ get ()
_ Set () assigns values to the protected or private attributes.
_ Set ($ name, $ val );
_ Get () gets the value of protected or private
_ Get ($ name );
For example:Copy codeThe Code is as follows: <? Php
Class testa {
Protected $ name;
// Use _ set () to manage all attributes
Public function _ set ($ pro_name, $ pro_val ){
// The above $ pro_name and $ pro_val can be customized
// The following $ this-> pro_name is set and cannot be changed
$ This-> pro_name = $ pro_val;
}
// Use _ get () to obtain all attribute values
Public function _ get ($ pro_name ){
If (isset ($ pro_name )){
Return $ this-> pro_name;
} Else {
Return null;
}
}
}
$ N1 = new testa ();
// Normally, the protected attribute cannot be accessed outside the class, but the above method can be used to operate them.
$ N1-> name = 'h3c ';
Echo $ n1-> name;
?>

// The above code can be understood and is not recommended.
Ii. Inheritance
Let's take a look at an example:Copy codeThe Code is as follows: <? Php
Class Pupil {
Public $ name;
Protected $ age;
Public function getinfo (){
Echo $ this-> name. '|'. $ this-> age;
}
Public function testing (){
Echo 'this is pupil ';
}
}
Class Graduate {
Public $ name;
Protected $ age;
Public function getinfo (){
Echo $ this-> name. '|'. $ this-> age;
}
Public function testing (){
Echo 'this is graduate ';
}
}
?>

As can be seen from the above example, when multiple classes have many common attributes and methods, code reusability is not high, code redundancy, and the processing methods in css are considered.
Solution: InheritCopy codeThe Code is as follows: <? Php
Class Students {
Public $ name;
Public $ age;
Public function _ construct ($ name, $ age ){
$ This-> name = $ name;
$ This-> age = $ age;
}
Public function showinfo (){
Echo $ this-> name. '|'. $ this-> age;
}
}
Class Pupil extends Students {
Function testing (){
Echo 'pupil '. $ this-> name.' is testing ';
}
}
Class Graduate extends Students {
Function testing (){
Echo 'graduate'. $ this-> name. 'is testing ';
}
}
$ Stu1 = new Pupil ('zhang san', 20 );
$ Stu1-> showinfo ();
Echo '<br/> ';
$ Stu1-> testing ();
?>

As can be seen from the above, inheritance is a sub-class (Subclass) that uses the extends parent class to continue the attributes and methods of public and protected in the parent class (BaseClass), and cannot inherit private attributes and methods.
Syntax structure:
Class Parent class name {}
Class subclass name extends parent class name {}
Details:
1. A subclass can inherit only one parent class (here it refers to direct inheritance). If you want to inherit attributes and methods of multiple classes, you can use multi-layer inheritance.
Example:Copy codeThe Code is as follows: <? Php
Class {
Public $ name = 'aaa ';
}
Class B extends {
Public $ age = 30;
}
Class C extends B {}
$ P = new C ();
Echo $ p-> name; // output AAA
?>

2. When creating a subclass object, the constructor of its parent class is not automatically called by default.
Example:
Class {
Public function _ construct (){
Echo 'a ';
}
}
Class B extends {
Public function _ construct (){
Echo 'B ';
}
}
$ B = new B (); // The constructor in B is output first. If B does not contain constructor, the constructor IN A is output.
3. If you need to access the method of the parent class in the subclass (the constructor and member method modifier are protected or private), you can use the parent class: method name or parent:: method name to complete [both parent and self mentioned previously are in lower case, and an error is reported in upper case]
Class {
Public function test (){
Echo 'A _ test ';
}
}
Class B extends {
Public function _ construct (){
// Both methods are supported.
A: test ();
Parent: test ();
}
}
$ B = new B ();
5. If the method of a subclass (derived class) is exactly the same as that of the parent class (public, protected), we call it method override or method override (override). See the following polymorphism.
Iii. Polymorphism
Example:Copy codeThe Code is as follows: <? Php
Class Animal {
Public $ name;
Public $ price;
Function cry (){
Echo 'I don \'t know ';
}
}
Class Dog extends Animal {
// Overwrite and overwrite
Function cry (){
Echo 'wang Wang! ';
Animal: cry (); // No error is reported here. The cry () of the parent class can be correctly executed ();
}
}
$ Dog1 = new Dog ();
$ Dog1-> cry ();
?>

Summary:
1. When a parent class knows that all child classes have a method, but the parent class cannot determine how to write the method, the Child class can overwrite the method and overwrite the method ), the method names and parameters of subclass must be exactly the same.
2. If the subclass needs to call a method (protected/public) of the parent class, you can use the parent class name: method name or parent: method name.
3. Access modifiers can be different during method rewriting, however, the access permission of the subclass method must be greater than or equal to that of the parent method (that is, the access permission of the parent method cannot be reduced)
For example, if the parent class public function cry () {} subclass protected function cry () {}, an error is returned.
However, the sub-class access permission can be enlarged, for example:
The parent class private function cry () {} subclass protected function cry () {} can be correctly executed.
Extension:
Method overload)
Basic concept: function names are the same, but the number or type of parameters are different. To call the same function, different functions can be distinguished.
Although php 5 also supports overloading, it is quite different from other languages. php cannot define multiple functions with the same name.
PHP5 provides powerful "magic" functions. Using these magic functions, we can achieve function overloading,
Here we will go to _ call. When an object calls a method and the method does not exist, the program will automatically call _ call
[Not officially recommended]
PHP has the following magic constants: __line _ FILE _ DIR _ FUNCTION _ CLASS _.
Example:Copy codeThe Code is as follows: <? Php
Class {
Function test1 ($ p ){
Echo 'test1 <br/> ';
}
Function test2 ($ p ){
Echo 'test2 <br/> ';
}
Function _ call ($ method, $ p ){
// Here $ p is an array, and the above two variable names can be customized
If ($ method = 'test '){
If (count ($ p) = 1 ){
$ This-> test1 ($ p );
} Else if (count ($ p) = 2 ){
$ This-> test2 ($ p );
}
}
}
}
$ A = new ();
$ A-> test (5 );
$ A-> test (3, 5 );
?>

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.