Extension keywords of PHP classes

Source: Internet
Author: User
Inheritance of PHP classes extends keywords inheritance of PHP classes

The inheritance of PHP classes refers to the creation of a new derived class that inherits data and methods from one or more previously defined classes and can be redefined or added to new data and methods, thus, the level or level of the class is established.

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

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

Note: Unlike Java and other languages, in PHP, a class can only inherit data from one class directly, that is, single inheritance.

Use the extends keyword to define class inheritance:

Class subclass extends parent class {}

Example:

 Name ."
"; Echo" My age is :". $ this-> age ;}}// class inheritance class Student extends Person {var $ school; // attributes of the Student's school function study () {echo "My name is :". $ this-> name."
"; Echo" I'm ". $ this-> school. "Learning" ;}}$ t1 = new Student (); $ t1-> name = "zhang san"; $ t1-> school = "Renmin University "; $ t1-> study ();?>

Run this example and output:

My name is Zhang San. I am studying at Renmin University.

In software development, the inheritance of classes makes the software open and scalable. this is an effective method for information organization and classification. it simplifies the workload of object and class creation, added code reusability. Inheritance is used to provide a standard class level structure. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.

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.