Home  >  Article  >  Backend Development  >  PHP object-oriented syntax 3 inheritance extends

PHP object-oriented syntax 3 inheritance extends

黄舟
黄舟Original
2016-12-28 10:07:591283browse

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.&#39;人民币&#39;;
    }
}
$book1=new  Books;
$book1->sayName(&#39;php开发&#39;);
$book1->sayPrice(&#39;156.47&#39;);

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.&#39;没有货币单位&#39;;
    }
    }
class Books extends Goods{
public function sayPrice($price){
    $this->price=$price;
    echo $this->price.&#39;人民币&#39;;
    }
}
//$good=new Goods;
//$good->sayPrice(&#39;96.47&#39;);
echo &#39;<hr/>&#39;;
$book1=new  Books;
$book1->sayPrice(&#39;156.47&#39;);

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 = &#39;ITCAST&#39;;//名字
    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(&#39;phpphpphp&#39;,88.8,&#39;new&#39;,&#39;Bejjing publisher&#39;);
    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 (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn