Week 20, year 2011
- Lazy Loading in PHP with Closures - Closures are a great way to do all kinds of neat tricks in PHP, and they’re particularly useful for Lazy Loading. I’m currently involved in a +200k SLoC legacy project, and the challenge is moving it to a Domain Driven implementation (while improving the performance), with the ultimate goal of making it more testable. The problem We want to find a Customer, and ask it for a list of Orders: <?php// client code$customer=$customerRepository->find($id);$orders=$customer->getOrders(); With the ActiveRecord pattern, this is simple. The Customer object holds an instance of the database adapter, and queries it for related Orders: <?phpclassCustomer{publicfunctiongetOrders(){$ordersData=$this->db->query(/* select orders. . . [Mathias Verraes]