Родительский класс
class ShopProduct
{
public $name;
public $price;
public function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
public function getProduct()
{
return $this->name.'-'.$this->price;
}
}
echo (new ShopProduct("Book", "200"))->getProduct();
Результатом выполнения будет строка
Book-200
Дочерний класс
class shopProductBook extends ShopProduct
{
public $numPages;
public function __construct(string $name, float $price, int $numPages)
{
parent::__construct($name, $price);
$this->numPages = $numPages;
}
public function getProduct()
{
return parent::getProduct().'pages: '.$this->numPages;
}
}
echo (new shopProductBook("Book", 200.5, 500))->getProduct();
Результатом выполнения будет строка
Book-200.5pages: 500