PHP class Inheritance extends keyword

Source: Internet
Author: User

Inheritance of PHP classes

The inheritance of a PHP class refers to the creation of a new derived class that inherits data and methods from one or more previously defined classes, and can redefine or add new data and methods to establish the hierarchy or rank of the class.

We call the existing class to derive the new class as the parent class, and the new class derived from the existing class as a subclass. Inheritance is one of the three main features of object-oriented.

The inheritance mechanism allows you to define new data types by using existing data types. The new data type that you define has not only the newly defined members, but also the old members.

Note: Unlike languages such as Java, in PHP, a class can inherit data directly from a class, that is, single inheritance.

Use the extends keyword to define inheritance for a class:

Class Subclass extends parent class {}

Example:

 
  Name. "
"Echo" My Age is: ". $this->age; }} Classes inherit class Student extends person { var $school; Student's School attribute function study () { echo "My name is called:". $this->name. "
"; echo "I am". $this->school. " Learning "; }} $t 1 = new Student (); $t 1->name = "Zhang San"; $t 1->school = "Renmin University"; $t 1->study ();? >

To run the example, output:

My name is called: Zhang San I am studying at Renmin University

In software development, the inheritance of the class makes the software open and extensible, which is an effective method of information organization and classification, which simplifies the creation of objects and classes, and increases the weight of the code. With inheritance, the hierarchical structure of the class's specification is provided. Through the inheritance of the class, the common features can be shared, and the reusability of the software is improved.

  • Related Article

    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.