- method_exists: documentation ( source)
<?php
class Manufacturer{
protected $_name;
public function setManufacturerName($name){
$this->_name = $name;
}
public function getManufacturerName(){
return $this->_name;
}
}
class Product{
protected $_title;
protected $_category;
protected $_manufacturer;
public function __construct(){
$this->_manufacturer = new Manufacturer();
}
public function getCategory(){
return $this->_category;
}
public function getTitle(){
return $this->_title;
}
public function __clone(){
$this->_manufacturer = clone $this->_manufacturer;
}
public function __call($method, $arguments){ // replaces setManufacturerName() and getManufacturerName() wich do not exist in current class
if (method_exists($this->_manufacturer, $method)) {
return call_user_func_array(array($this->_manufacturer , $method), $arguments);
}
}
/*public function setManufacturerName($name){
$this->_manufacturer->setManufacturerName($name);
}*/
/*public function getManufacturerName(){
return $this->_manufacturer->getManufacturerName();
}*/
}
class Order extends Product{
protected $_price;
protected $_deliver;
public function __construct($title, $category, $price){
parent::__construct();
$this->_title = $title;
$this->_category = $category;
$this->_price = $price;
$this->doWeDeliver();
}
public function setTitle($title){
$this->_title = $title;
}
public function getPrice(){
return $this->_price;
}
public function getDeliver(){
return $this->_deliver;
}
protected function doWeDeliver(){
$this->_deliver = $this->_price > 30 ? "Yes" : "No" ;
}
public function displaySentence(){
echo "Product: ".$this->_title." Category: ".$this->_category." Prijs: ".$this->_price." Gratis bezorging: ".$this->_deliver;
}
}
$order = new Order("CD Brahms", "Media", "45");
$order->setManufacturerName("Producent A");
echo $order->getTitle()." is made by ". $order->getManufacturerName()."<br />"; // CD Brahms is made by Producent A
$order2 = clone $order;
$order2->setTitle("Second title");
$order2->setManufacturerName("Producent B");
echo $order2->getTitle()." is made by ". $order2->getManufacturerName()."<br />"; // Second title is made by Producent B
echo $order->getTitle()." is made by ". $order->getManufacturerName()."<br />"; // CD Brahms is made by Producent A