Home > Article > Backend Development > Preliminary understanding of PHP - encapsulation, inheritance, polymorphic features
In the last basic article, K introduced you to the use of arrays and strings in PHP. This time, K decided to give it a one-time release and share with you the object-oriented methods in PHP. Three major features: knowledge in three aspects: encapsulation, inheritance, and polymorphism.
In PHP, encapsulation can be summarized in three sub-sections: privatization. Specifically, access control is achieved by privatizing properties and methods in the class that do not require external access through access modifiers.
So what is the function of encapsulation? There are two main functions of encapsulation. One is method encapsulation, which exposes the functions that users care about and hides functions that other users cannot use; the other is property encapsulation, that is Control user data to prevent illegal data transmission settings.
So how is the encapsulation operation implemented?
For method encapsulation, we set it by adding the private keyword before the method, that is, privatization. The demo is as follows:
private function formatName(){} // 这个方法仅仅能在类内部使用$this调用 function showName(){ $this -> formatName(); }
For attribute encapsulation, we can implement it in two ways.
First, set/read privatized properties through the set/get methods provided by itself. The demo is as follows:
private $age; function setAge($age){ $this -> age = $age; } function getAge(){ return $this -> age; }
The second isimplemented through magic methods. The demo is as follows:
private $age;function __get($key){return $this -> $key; }function __set($key,$value){$this -> $key = $value; }$对象 -> age; >>> 访问对象私有属性时,自动调用__get()魔术方法,并且将访问的属性名传给__get()方法;$对象 -> age = "11"; >>> 设置对象的私有属性时,自动调用__set()魔术方法,并且将设置的属性名以及属性值传给__set()方法;
In addition, the magic methods related to encapsulation mainly include the following:
① __set( ): Automatically called when assigning values to class private attributes. When calling, pass two parameters to the object, the attribute name and attribute value that need to be set
② __get(): Automatically called when reading class private attributes , pass a parameter to the method when calling, the attribute name that needs to be read
③ __isset(): Automatically called when the isset() function is used externally to detect private attributes.
>>> Use isset() outside the class to detect private attributes, which are not detected by default. That is false
>>>So, we can use the __isset() function to return the internal detection result when automatically called.
Function __isset($key){
Return isset($this->$key);
}
When external isset($object name->private attribute) is used for detection, the result returned by the above __isset() will be automatically called!
④ __unset(): Automatically called when the unset() function is used externally to delete private attributes.
Function __unset($key){
unset($this->$key);
}
When an attribute is deleted externally using unset($object name->private attribute), the attribute name is automatically passed to __unset() and handed over to this magic method for processing.
## In PHP, inheritance can also use the word extends to summarize. Extends is used on a subclass to allow the subclass to inherit non-private properties and methods from the parent class. The demo is as follows:
class Student extends Person{}
但是,在使用继承时,需要注意以下几点:
① 子类只能继承父类的非私有属性。
② 子类继承父类后,相当于将父类的属性和方法copy到子类,可以直接使用$this调用。
③ PHP只能单继承,不支持一个类继承多个类,但是一个类可以进行多层继承。
>>> class Person{}
class Chengnian extends Person{}
class Student extends Chengnian{}
Student类就同时具有了Chengnian类和Person类的属性和方法
下面给大家介绍一下关于方法重写(方法覆盖)的一些知识。
方法覆盖是指子类对继承父类中重名方法的重写或覆盖,而它的实现需要满足两个条件,一是子类继承父类;二是子类重写父类已有方法。
那么如果我想要在调用父类中的方法,应该怎么做?我们可以通过如下代码实现:
parent::方法名();
同理,在子类继承父类时,首先也要调用父类的构造函数来赋值,实现代码如下:
function __construct($name,$age,$school){ parent::__construct($name,$age);$this -> school = $school; }// 其中name、age属性继承自父类,school属于子类
在PHP中,多态是最常用到的一种特性。所谓多态,是指同一个东西不同形态的展示。在PHP中,我们这样定义多态,一个类被多个子类继承,如果这个类的某个方法在多个子类中表现不同的功能,那么这种行为我们就称其为多态的实现。
多态的实现必须要满足三个条件:一是子类继承父类,二是子类重写父类的方法,三是父类引用指向子类对象。
多态的实现K用一个小小的demo来给大家进行演示,demo如下:
abstract class Person{abstract function say(); } class Chinese extends Person{ // 条件一:子类继承父类function say(){ // 条件二:子类重写父类方法echo "我说中文!<br>"; } }class English extends Person{ //条件一:子类继承父类function say(){ // 条件二:子类重写父类方法echo "I speak English!<br>"; } } // Person $c = new Chinese(); // 父类引用指向子类对象,PHP中此形式行不通 // Person $e = new English();function func(Person $p){ //PHP中的类型约束只存在与函数的形参$p -> say(); } func(new Chinese()); // 条件三:父类引用指向子类对象func(new English()); // 条件三:父类引用指向子类对象
好了,以上就是K给大家带来的关于PHP面向对象中封装、继承、多态这三个特性的初步理解,如果对你有帮助的话,可不要忘了给K点个赞哟~
The above is the detailed content of Preliminary understanding of PHP - encapsulation, inheritance, polymorphic features. For more information, please follow other related articles on the PHP Chinese website!