<?php
class Dish
{
protected $ingridients;
public function addIngridient($ingridient)
{
if($ingridient instanceof Ingridient)
$this->ingridients[] = $ingridient;
else
die('error');
}
public function showIngridients()
{
foreach ($this->ingridients as $ingridient)
{
echo 'Ingridient: '.$ingridient->name.'<br>';
}
}
}
class Ingridient
{
public $name;
public $price;
public function __construct($name, $price)
{
$this->name = $name;
}
//Код класса ингридиента
}
$soup = new Dish();
$soup->addIngridient( new Ingridient('water', '1$') );
$soup->addIngridient( new Ingridient('beef', '5$') );
$soup->addIngridient( new Ingridient('salt', '0.1$') );
$soup->showIngridients(); //нет ошибки
$soup->addIngridient('test'); //есть ошибка
$soup->showIngridients();