PHP object-oriented-encapsulation, inheritance, polymorphism, object-oriented Polymorphism
K introduced how to use arrays and strings in PHP in the last basic article. This time, K decided to send a large package at a time, I would like to share with you the three main object-oriented features of PHP: encapsulation, inheritance, and polymorphism.
I. Encapsulation
In PHP, encapsulation can be summarized as follows:Private. Specifically, access control is implemented by privatize the attributes and methods in the class that do not require external access through the access modifier.
Then the encapsulatedFunctionWhat is it? Encapsulation has two main functions: Method encapsulation, exposing the functions that users are concerned about, and hiding functions that other users cannot use; and attribute encapsulation, control user data to prevent invalid data transmission settings.
How is encapsulation implemented?
For method encapsulation, we add the private keyword before the method to set, that is, private processing. The demo is as follows:
Private function formatName () {}// this method can only use $ this within the class to call function showName () {$ this-> formatName ();}
We can implement attribute encapsulation in two ways.
First, you can set/read private attributes through the set/get method provided by yourself. The demo is as follows:
private $age; function setAge($age){ $this -> age = $age; } function getAge(){ return $this -> age; }
Second, implement it through magic. The demo is as follows:
Private $ age; function _ get ($ key) {return $ this-> $ key;} function _ set ($ key, $ value) {$ this-> $ key = $ value;} $ object-> age; >>> when accessing the private property of an object, the _ get () magic method is automatically called, and pass the accessed property name to the _ get () method; $ object-> age = "11"; >>> when setting the private property of an object, the _ set () magic method is automatically called, and the set attribute name and attribute value are passed to the _ set () method;
In addition, there are several major encapsulation magic methods:
① _ Set (): it is automatically called when a class private attribute is assigned a value. Two parameters are passed to the object during the call. The attribute name and attribute value to be set are as follows:
② _ Get (): it is automatically called when reading private properties of the class. A parameter is passed to the method during the call, and the attribute name to be read
③ _ Isset (): it is automatically called when the isset () function is used externally to detect private attributes.
>>> Private attributes cannot be detected by default when isset () is used outside the class. False
>>> Therefore, we can use the _ isset () function to return internal detection results during automatic calling.
Function _ isset ($ key ){
Return isset ($ this-> $ key );
}
When isset ($ Object Name-> private attribute) is detected externally, the results returned by _ isset () are automatically called!
④ _ Unset (): it is automatically called when the unset () function is used externally to delete private attributes.
Function _ unset ($ key ){
Unset ($ this-> $ key );
}
When unset ($ Object Name-> private attribute) is used to delete an attribute, the attribute name is automatically passed to _ unset () and handled by this magic method.
Ii. Inheritance
In PHP, inheritance can also use wordsExtends. Extends is used on subclasses to inherit non-private attributes and methods in the parent class. The demo is as follows:
class Student extends Person{}
However, when using inheritance, you mustNote:The following points:
① Subclass can only inherit non-private attributes of the parent class.
② After the subclass inherits the parent class, it is equivalent to copying the attributes and methods of the parent class to the subclass and can be called directly using $ this.
③ PHP can only inherit one class. It does not support one class to inherit multiple classes, but one class can inherit multiple layers.
>>> Class Person {}
Class Chengnian extends Person {}
Class Student extends Chengnian {}
The Student class has both attributes and methods of the Chengnian class and the Person class.
The following describes howMethod Rewriting(Method coverage) knowledge.
Method override refers to the rewriting or overwriting of the methods that inherit the names of the parent class. Its implementation must meet two conditions. One is that the Child class inherits the parent class; second, the subclass overrides existing methods of the parent class.
So what should I do if I want to call methods in the parent class? We can use the following code:
Parent: Method Name ();
Similarly, when the subclass inherits the parent class, you must first call the constructor of the parent class to assign values. The implementation code is as follows:
Function _ construct ($ name, $ age, $ school) {parent ::__ construct ($ name, $ age); $ this-> school = $ school ;} // The name and age attributes inherit from the parent class, and the school belongs to the subclass.
Iii. Polymorphism
Polymorphism is the most common feature in PHP. The so-called polymorphism refersDisplay of different forms of the same thing. In PHP, we define polymorphism in this way. A class is inherited by multiple subclasses. If a method of this class represents different functions in multiple subclasses, this behavior is called the implementation of polymorphism.
The implementation of polymorphism must meet three conditions: first, subclass inherits the parent class, second, subclass overrides the parent class, and third, parent class references point to the subclass object.
The implementation of polymorphism K uses a small demo for demonstration. The demo is as follows:
Abstract class Person {abstract function say ();} class Chinese extends Person {// condition 1: subclass inherits the parent class function say () {// condition 2: subclass override parent class method echo "I speak Chinese! <Br> ";}} class English extends Person {// condition 1: The subclass inherits the parent class function say () {// condition 2: subclass override parent class method echo "I speak English! <Br> ";}}// Person $ c = new Chinese (); // The parent class references a subclass object, this form does not work in PHP // Person $ e = new English (); function func (Person $ p) {// The type constraint in PHP only exists with the function parameter $ p-> say ();} func (new Chinese (); // Condition 3: parent class references to the subclass object func (new English (); // Condition 3: parent class references to the subclass object
Well, the above is a preliminary understanding of the three features of PHP object-oriented encapsulation, inheritance, and polymorphism. If it is helpful to you, don't forget to give K a thumbs up ~