PHP classes and objects: inheritance

Source: Internet
Author: User
Tags php basics

Inherited:

One of the greatest advantages of object-oriented programming is that one class can inherit and have a member variable and member method of another existing class, where the inherited class is called the parent class, and the inherited class is called a subclass. The subclass inherits the parent class and obtains all the member variables and member methods of the parent class. Inheritance can improve the reusability of code and maintainability.

Through the inheritance of classes, subclasses can not only have newly defined member variables and member methods, but also have all member variables and member methods of the parent class. The syntax format is as follows:

class子类名称 extends父类名称 {    //子类的成员变量列表    function成员方法1([参数1,参数2,……]){    //成员方法1的具体实现    }    function成员方法2([参数1,参数2,……]){    //成员方法2的具体实现    }//其他成员方法}

The following creates a student class that inherits from the already existing people class, with the following code:

classPeople{    function __construct($name,$age){        $this->name=$name;        $this->age=$age;         }    functionsay(){        echo"姓名:".$this->name." ";        echo"年龄:".$this->age;        echo" ";    }    function__destruct(){}}classStudent extendsPeople {    functionstudy($lang){        echo"我正在整理".$lang."……";    }}$student=newStudent("大兵",25);$student->say();$student->study("PHP基础知识");

The result of the code execution is:
Name: Soldier Age: 25
I'm working on the basics of PHP ...

Parent:: keyword

Keyword Parent:: Used to represent the parent class and the member method in the parent class to invoke, the syntax is as follows:

parent::父类的成员方法([参数1,参数2,……])

The following example uses the parent:: keyword to invoke the constructor method in people, the code is as follows:

classPeople{    function__construct($name){        $this->name=$name;       }    function__destruct(){}}classStudent extendsPeople {    function__construct($name,$age){        parent::__construct($name);        $this->age=$age    }    functionsay(){    echo"姓名:".$this->name." ";    echo"年龄:".$this->age;    echo" ";    }    function study($lang){        echo"我正在整理".$lang."……";    }    function__destruct(){}}$student=newStudent("大兵",25);$student->say();$student->study("PHP基础知识");

The result of the code execution is:
Name: Soldier Age: 25
I'm working on the basics of PHP ...

instanceof operator

Detects whether the current object instance belongs to a class you can use the instanceof operator, which returns a Boolean value with the following syntax:

对象名称 instanceof类名称

overriding the parent class method

Methods that inherit from the parent class can be overridden in a subclass, also called methods, as needed. The overriding method and the overridden method require the same method name, parameter list, and return value type for method overrides.
The following is an example of a say () method that implements a subclass student overriding the parent class people, with the following code:

classPeople{    function__construct($name,$age){        $this->name=$name;        $this->age=$age;         }    functionsay(){        echo"姓名:".$this->name." ";        echo"年龄:".$this->age;        echo " ";    }    function__destruct(){}}classStudent extendsPeople {    functionsay($lang){        echo$this->name. "正在整理".$lang."……";    }}$student=newStudent("大兵",25);$student->say("PHP基础知识");

The result of the code execution is:
The Marines are tidying up PHP basics ...

PHP classes and objects: inheritance

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.