PHP Object-Oriented Learning Objectives: fully understand the three main features of abstraction, encapsulation, inheritance, and multi-state image targeting: encapsulation, inheritance, and Polymorphism. First of all, a brief understanding of abstraction:
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:
The code is as follows:
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:
The code is as follows:
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:
The code is as follows:
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: Inherit
The code is as follows:
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'
';
$ 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:
The code is as follows:
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:
The code is as follows:
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:
The code is as follows:
Class {
Function test1 ($ p ){
Echo 'test1
';
}
Function test2 ($ p ){
Echo 'test2
';
}
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 );
?>