PHP Object-oriented Syntax 3 inheritance extends

Source: Internet
Author: User
Tags php class
Inheritance: If an object A, using a member of another object B, then we will say that the A object inherits the B Object!
Tip: The concept of inheritance is embodied in the object, and the syntax is embodied in class B extends A {}!

<?php class goods{public        $goodsName;    public $price;    Public Function Sayname ($goodsName) {        $this->goodsname= $goodsName;        echo $this->goodsname;    }    } Class Books extends Goods{public function Sayprice ($price) {    $this->price= $price;    echo $this->price. ' RMB ';    }} $book 1=new  Books; $book 1->sayname (' PHP development '); $book 1->sayprice (' 156.47 ');

The grammatical meaning is, in object-oriented syntax, code reuse!
Instanceof, whether it is a class instance. (The concept of instanceof and +-*/is the same as the operator)

<?php class amparent{    }class amchild extends amparent{    } $amChild =new  amchild;var_dump ($amChild instanceof Amchild); Var_dump ($amChild instanceof amparent);

Result of Operation:
BOOL (TRUE)
BOOL (TRUE)


Override, override. is a phenomenon that only inheritance can occur (using or avoiding the situation)
If the child class, with the parent class, has a member (property, method) with the same name, then when instantiating the subclass object, only the members defined in the subclass are given, called rewriting!
Tip
Overrides are not replacements!
Two different sayprice are present. We get the properties or methods we see through the Book class object, similar to the process of looking up to the nearest position.

<?php class goods{public    $goodsName;    public $price;    Public Function Sayprice ($price) {    $this->price= $price;    echo $this->price. ' No currency unit ';    }    } Class Books extends Goods{public function Sayprice ($price) {    $this->price= $price;    echo $this->price. ' RMB ';    }} $good =new goods;//$good->sayprice (' 96.47 '); Echo ' 

Operation Result:

¥ 156.47

Parent, parental class
Once overridden, the parent code is not executed!
A method with the same name as the parent subclass can be overridden, so some methods are bound to be overridden, such as construction methods!

<?php class Goods {public    $goods _name = ' itcast ';//Name Public    $goods _price;//commodity price public    function __ Construct ($name, $price) {        $this->goods_name = $name;        $this->goods_price = $price;    }} Class Book extends Goods {public    $author;//author public    $publisher;//publishing house public    function __construct ($name, $ Price, $author, $publisher)     {        Parent:: __construct ($name, $price);        $this->author = $author;        $this->publisher = $publisher;    }}    $book 1=new Book (' phpphpphp ', 88.8, ' new ', ' bejjing publisher ');    Var_dump ($book 1);

Result of Operation:

Object (book) #1 (4) {["Author"]=> string (3) "New" ["publisher"]=> string "bejjing publisher" ["Goods_name"]=& Gt String (9) "phpphpphp" ["Goods_price"]=> Float (88.8)}

The construction method of the parent class appears to be just a common method in the subclass.

The above is the PHP object-oriented Syntax 3 inheritance extends content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.