Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2014 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/60761e6fb80135cabb06 to your computer and use it in GitHub Desktop.
Save anonymous/60761e6fb80135cabb06 to your computer and use it in GitHub Desktop.
<?php
class Order
{
/**
* @var OrderLine[]
*/
private $orderLines = [];
/**
* @param string $name
* @param float $quantity
*/
public function addOrderLine($name, $quantity)
{
$this->orderLines = new OrderLine($name, $quantity);
}
}
class OrderLine
{
/**
* @var string
*/
private $name;
/**
* @var float
*/
private $quantity;
/**
* @param string $name
* @param float $quantity
*/
public function __construct($name, $quantity)
{
$this->name = $name;
$this->quantity = $quantity;
}
}
// Default Order usage as it was intended to use
$sampleOrder = new Order();
$sampleOrder->addOrderLine("Product A", 1.0);
$sampleOrder->addOrderLine("Product B", 1.0);
// Now we have a special requirement: our order lines now must include a source
class CustomOrderLine extends OrderLine
{
/**
* @var string
*/
private $source;
/**
* @param string $name
* @param float $quantity
* @param string $source
*/
public function __construct($name, $quantity, $source)
{
parent::__construct($name, $quantity);
$this->source = $source;
}
}
// But we cannot change the signature of `addOrderLine`, as PHP will throw a
// strict warning about a method signature mismatch
class CustomOrder extends Order
{
// !!! Notice Declaration of (...) should be compatible with that of (...)
public function addOrderLine($name, $quantity, $source)
{
...
}
}
// So, can't we extend `Order`? Do we have to implement all methods from scratch?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment