As we all know that php does not support multiple inheritance, but we can acheive it through some extent by writing our code. suppose we have a absract class Ingredents -
abstract class Ingredents {
private $DishId;
abstract public function requirement();
public function __construct($DishId) {
$this->DishId = $DishId;
}
public function getDishId() {
return $this->DishId;
}
}
Now If we want to make milk cake we can extend it simply -
class Milkcake extends Ingredents {
private $req = array();
public function __construct($req){
$this->req = $req;
}
public function requirement() {
return $this->req;
}
}
And if we want to make kesar rice then also we can get it simply
class KesarRice extends Ingredents {
private $req = array();
public function __construct($req) {
$this->req = $req;
}
public function requirement() {
return $this->req;
}
}
But now if we want to make kheer then ? ok we are going to use property of Milkcake and kesar rice both.
class Kheer {
private $req = array();
private $dishes = array();
public function requirement() {
foreach ($this->dishes as $dishes) {
$this->req[] = $dishes->requirement();
}
}
public function setDishes($dishes) {
$this->dishes = $dishes;
}
public function getRequirement() {
return $this->req ;
}
}
Now Execute the code -
$kesar_rice = new KesarRice(array('Rice','kesar'));
$kesar_rice_requirement = $kesar_rice->requirement();
print_r($kesar_rice_requirement);
$milk_cake = new Milkcake(array('Milk','sugar'));
$milk_cake_requirement = $milk_cake->requirement();
print_r($milk_cake_requirement);
$kheer = new Kheer();
$kheer->setDishes(array(
'KesarRice' => new KesarRice(array('Rice','kesar')),
'Milkcake' => new Milkcake(array('Milk','sugar'))
));
$kheer->requirement();
$kheer_requirement = $kheer->getRequirement();
print_r($kheer_requirement);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment