Skip to content

Instantly share code, notes, and snippets.

@kevinjqiu
Last active December 19, 2015 19:59
Show Gist options
  • Save kevinjqiu/6010369 to your computer and use it in GitHub Desktop.
Save kevinjqiu/6010369 to your computer and use it in GitHub Desktop.
<!-- product.php -->
<?php
class Product {
function __construct() {
}
}
?>
<!-- client.php -->
<?php
class Client {
public function __construct() {
$this->product = new Product(); <-- this is bad, because we can't mock it out in test
}
}
?>
<!-- factory.php -->
<?php
class Factory {
public function produce() {
return new Product();
}
}
?>
<!-- client2.php -->
<?php
class Client2 {
public function __construct($factory=new Factory()) {
$this->product = $factory->produce();
}
}
?>
<!-- test_client2.php -->
<?php
new Client2($someMockFactory);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment