PHP Object-oriented-encapsulation, inheritance, polymorphism

Source: Internet
Author: User

K in the last basic article about how to use the array and string in PHP, this time, K decided to make a one-off, to share the three main object-oriented PHP features: encapsulation, inheritance, polymorphism three aspects of knowledge.

first, the package

In PHP, the package can be summed up in three sub-terms: privatization . Specifically, access control is implemented by using the access modifier to privatize properties and methods in the class that do not require external access.

So what is the role of encapsulation? The role of encapsulation is mainly two points, one is the method encapsulation , will be the user's attention to the function of exposure, while hiding other users do not use the function, and the second is the attribute encapsulation , that is, users of the data control, to prevent illegal data transmission settings.

So how is the encapsulation operation implemented?

For the encapsulation of the method, we set it up by adding the private keyword to the method, that is, the privatization process. The demo is as follows:

Private function // This method can only be used within the class using the $this call         function ShowName () {             $this - formatname ();         }

  For the encapsulation of attributes, there are two ways we can implement them.

The first is to set/Read the properties of privatization by the Set/get method that you provide . The demo is as follows:

Private $age ;          function setage ($age) {             $this$age;         }          function Getage () {             return$this , age ;         

  Second, through the Magic method to achieve . 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 called automatically, and the Access property name is passed to the __get () method;$ Object-age = "11"; >>> when setting the private property of an object, the __set () Magic method is called automatically, and the property name and the property value of the set are passed to the __set () method;

  In addition, the Magic method for encapsulation mainly has the following kinds:

①__set (): Automatically called when assigning a value to a class private property, passing two arguments to the object when called, the property name to set, the property value
②__get (): Automatically called when the class private property is read, a parameter is passed to the method when called, and the property name that needs to be read
③__isset (): called automatically when a private property is detected externally using the Isset () function.
The >>> external uses isset () to detect private properties, which are not detected by default. That is false
>>> So, we can use the __isset () function to return internal detection results when automatically called.
function __isset ($key) {
return Isset ($this-$key);
}
The results returned by the above __isset () are automatically called when external use of the Isset ($ object name-to-private property) is detected!
④__unset (): The external use of the unset () function is called automatically when a private property is removed.
function __unset ($key) {
unset ($this, $key);
}
When a property is removed externally using the unset ($ object Name--private property), the property name is automatically passed to __unset () and is handled by this magic method.

Ii. Inheritance

In PHP, inheritance can also be summed up with the word extends . Extends is used on subclasses to allow subclasses to inherit non-private properties and methods from the parent class. The demo is as follows:

class extends person{}

  However, when using inheritance, you need to be aware of the following points:

The ① subclass can inherit only the non-private properties of the parent class.
After the ② subclass inherits the parent class, it is equivalent to copy the parent class's properties and methods to the subclass, which can be called directly using the $this.
③php can only be single-inherited and does not support a class inheriting multiple classes, but a class can inherit several layers.
>>> class person{}
Class Chengnian extends person{}
Class Student extends chengnian{}
The student class has both the properties and methods of the Chengnian class and the person class

Let me introduce you to some of the knowledge about method overrides (method overrides).

A method override is a subclass that overrides or overrides a method that inherits the same name in the parent class, and its implementation needs to satisfy two conditions, the subclass inherits the parent class, and the subclass overrides the existing method of the parent class.

So what should I do if I want to invoke a method in the parent class? We can do this with the following code:

Parent:: Method name ();

Similarly, when the subclass inherits the parent class, the constructor of the parent class is called first to assign the value, and the implementation code is as follows:

function __construct ($name,$age,$school) {    parent:: __construct ( $name,$age);     $this $school ;} // where the name, age attribute is inherited from the parent class, school belongs to the subclass
Three, polymorphic

In PHP, polymorphism is one of the most commonly used features. The so-called polymorphic, refers to the same thing in different forms of display . In PHP, we define polymorphism, a class is inherited by multiple subclasses, and if a method of this class behaves differently in multiple subclasses, then this behavior is called a polymorphic implementation.

The implementation of polymorphism must satisfy three conditions: first, the subclass inherits the parent class , and the subclass overrides the parent class's method , and the third is the parent class reference to the child class object .

Multi-state implementation K with a small demo to give you a demo, the demo is as follows:

    Abstract classperson{Abstract functionsay (); }        classChineseextendsperson{//Condition One: Subclass Inherits parent classfunctionsay () {//Condition two: Subclass overrides parent class methodEcho"I speak Chinese!" <br> "; }    }    class中文版extendsperson{//Condition One: Subclass Inherits parent classfunctionsay () {//Condition two: Subclass overrides parent class methodEcho"I speak English!<br>"; }    }    //Person $c = new Chinese ();//Parent reference to child class object, this form in PHP does not work//person $e = new 中文版 ();        functionFunc (person$p){//the type constraint in PHP only exists with the function's formal parameter        $p-say (); } func (NewChinese ());//condition Three: Parent class reference to child class objectFuncNew中文版 ());//condition Three: Parent class reference to child class object

OK, the above is the K to everyone to bring about the PHP object-oriented encapsulation, inheritance, polymorphism of the three characteristics of the preliminary understanding, if you have the help, do not forget to give K a praise yo ~

PHP Object-oriented-encapsulation, inheritance, polymorphism

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.