Inheritance: If an object A uses members of another object B, then we say that the A object inherits the B object!
tip: The concept of inheritance is reflected in the object, and the syntax is reflected in the class 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.'人民币'; } } $book1=new Books; $book1->sayName('php开发'); $book1->sayPrice('156.47');
The meaning of syntax is code reuse in object-oriented syntax!
instanceof, whether it is an instance of a certain type. (Instanceof has the same concept as +-*/, it is an operator)
<?php class AmParent{ } class AmChild extends AmParent{ } $amChild=new AmChild; var_dump( $amChild instanceof AmChild); var_dump( $amChild instanceof AmParent);
Operation result:
bool(true)
bool(true)
Rewrite, override. This is a phenomenon, only inheritance will occur (take advantage of or avoid this situation)
If the subclass and the parent class have members (properties, methods) with the same name, when instantiating the subclass object, you will only get the subclass Defined members, call it an override!
tip:
Rewriting is not replacement!
Two different sayprices exist. We get the currently seen properties or methods through the Book class object, which is similar to the process of searching upward for the nearest location.
<?php class Goods{ public $goodsName; public $price; public function sayPrice($price){ $this->price=$price; echo $this->price.'没有货币单位'; } } class Books extends Goods{ public function sayPrice($price){ $this->price=$price; echo $this->price.'人民币'; } } //$good=new Goods; //$good->sayPrice('96.47'); echo '<hr/>'; $book1=new Books; $book1->sayPrice('156.47');
Run result:
156.47 RMB
parent, parent class
Once rewritten, the parent class code will no longer be executed!
The method with the same name of the parent class and subclass will be overridden, so some methods will definitely be rewritten, such as the construction method!
<?php class Goods { public $goods_name = 'ITCAST';//名字 public $goods_price;//商品价格 public function __construct($name, $price) { $this->goods_name = $name; $this->goods_price = $price; } } class Book extends Goods { public $author;//作者 public $publisher;//出版社 public function __construct($name, $price,$author,$publisher) { parent:: __construct($name, $price); $this->author = $author; $this->publisher = $publisher; } } $book1=new Book('phpphpphp',88.8,'new','Bejjing publisher'); var_dump($book1);
Operation result:
object(Book)#1 (4) { [“author”]=> string(3) “new” [“publisher”]=> string(17) “Bejjing publisher” [“goods_name”]=> string(9) “phpphpphp” [“goods_price”]=> float(88.8) }
The constructor method of the parent class is just an ordinary method in the view of the subclass.
The above is the content of PHP object-oriented syntax 3 inheritance extends. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!