Php object-oriented: class inheritance examples

Source: Internet
Author: User
To put it bluntly, I think it is to improve code efficiency. Next I will introduce the inheritance. The inherited subclass of a class inherits all member variable methods of the parent class, including the constructor. when the subclass is instantiated, php queries the constructor in the subclass, if the subclass has its own constructor method, php will first call the method in the subclass; if the subclass does not exist, php will call the constructor in the parent class, which is what we call inheritance. What is class inheritance?To put it bluntly, I think it is to improve code efficiency. Next I will introduce the inheritance.

Class inheritance

Subclass inherits all member variable methods of the parent class, including the constructor. when the subclass is instantiated, php queries the constructor in the subclass. if the subclass has its own constructor, php will first call the method in the subclass; if the subclass is not, php will call the constructor in the parent class, which is what we call inheritance.

The class is inherited by the keyword extends. syntax:

class A extends B{...}

A represents A subclass, and B represents A parent class.

Now that we understand the basic concepts, let's take a look at the inherited instance of the class.:

First, create a class with different methods:

 Name = $ name; $ this-> price = $ price;} function ShowInfo () {echo "not displayed ";}} // subclass Playclass Play extends Lol {// defines the subclass and inherits from the parent class public $ type; // defines the variable public function _ construct ($ name, $ type) in the subclass) {$ this-> name = $ name; $ this-> type = $ type;} function ShowInfo () {if ($ this-> type = "mid ") {return $ this-> name. "will play this location";} else {return $ this-> name. "won't Play this location" ;}}// instantiate the object $ player = new Play ("faker", "mid"); echo $ player-> ShowInfo ();

The above is php object-oriented: the details of the class inheritance instance. For more information, see other related articles in the first PHP community!

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.