Created
May 17, 2011 19:44
-
-
Save mathiasverraes/977230 to your computer and use it in GitHub Desktop.
Lazy Loading with Closures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// client code | |
$customer = $customerRepository->find($id); | |
$orders = $customer->getOrders(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Customer | |
{ | |
public function getOrders() | |
{ | |
$ordersData = $this->db->query(/* select orders... */); | |
$orders = array();) | |
foreach($ordersdata as $orderdata) { | |
$orders[] = new Order($orderdata); | |
} | |
return $orders; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Customer | |
{ | |
public function getOrders() | |
{ | |
return $this->orders; | |
} | |
} | |
class CustomerRepository | |
{ | |
public function find($id) | |
{ | |
$customerdata = $this->db->query(/* select customer ...*/); | |
$customer = new Customer($customerdata); | |
$ordersdata = $this->db->query(/* select orders ... */); | |
foreach($ordersdata as $orderdata){ | |
$customer->addOrder(new Order($orderdata)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Customer | |
{ | |
public function setOrdersReference(Closure $ordersReference) | |
{ | |
$this->ordersReference = $ordersReference; | |
} | |
public function getOrders() | |
{ | |
if(!isset($this->orders)) { | |
$reference = $this->ordersReference; | |
$this->orders = $reference(); | |
} | |
return $this->orders; | |
} | |
} | |
class CustomerRepository | |
{ | |
public function find($id) | |
{ | |
$db = $this->db; | |
$customerdata = $db->query(/* select customer ...*/); | |
$customer = new Customer($customerdata); | |
$ordersReference = function($customer) use($id, $db) { | |
$ordersdata = $db->query(/* select orders ... */); | |
$orders = array(); | |
foreach($ordersdata as $orderdata) { | |
$orders[] = new Order($orderdata); | |
} | |
return $orders; | |
}; | |
$customer->setOrderReference($ordersReference); | |
return $customer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aren't you missing a
return $orders;
in listing4.php?