Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Edd Mann:
Implementing Streams in PHP
Jan 16, 2015 @ 16:09:22

Edd Mann has a new post today looking at implementing streams in your PHP applications. In this case we're not talking about the streams built into PHP but the concept of a source of information that only produces the next item when requested (aka "lazy loading").

Typically, when we think about a list of elements we assume there is both a start and finite end. In this example the list has been precomputed and stored for subsequent traversal and transformation. If instead, we replaced the finite ending with a promise to return the next element in the sequence, we would have the architecture to provide infinite lists. Not only would these lists be capable of generating infinite elements, but they would also be lazy, only producing the next element in the sequence when absolutely required. This concept is called a Stream, commonly also referred to as a lazy list, and is a foundational concept in languages such as Haskell.

He talks about how streams of data should be interacted with differently than a finite list of data and the promises they're based on to provide the right data. He shows two different approaches to implementing a an object to stream data from - a class-based method and one that uses generators. Sample code is provided for each with the generator approach being a bit shorter as they're designed to lazy load items as requested.

tagged: stream data lazyload generator class iterator tutorial

Link: http://eddmann.com/posts/implementing-streams-in-php/

VG Tech Blog:
Lazy Loading Resources with Zend Framework Bootstrap
Jun 01, 2012 @ 19:48:23

On the VG Tech blog today André Roaldseth has a new post showing how to lazy load in the Zend Framework bootstrap using two handy methods you can drop into your applications initialization.

The Bootstrapping process in Zend Framework isn’t perfect. You’ll often end up bootstrapping a lot of resources that you don’t need to complete the request. Depending on the resources this can be really expensive and hurt your overall performance. The worst kind are resources that open connections to external services [...], even worse, if the services are down they will end up blocking the execution.

Using his modified "getResource" and "lazyload" methods, he shows you how to modify your resource requests to put them in a temporary state that is only initialized when the resource is needed. You can find the code for this example in this gist.

tagged: zendframework resource lazyload bootstrap

Link:

DevShed:
PHP Closures as View Helpers: Lazy-Loading File Data
Jan 30, 2012 @ 19:08:28

In the second part of their look at using closures in PHP as view helpers, DevShed improves upon their original code by adding some additional classes and using them in the closures.

The best way to show you how using anonymous functions can help you to develop more efficient OO applications is with some functional, hands-on examples. With this idea in mind, in the installment that preceded this one, I implemented an extendable template system. This system could spawn view objects and render the template files associated with these objects.

In this second part of the (two-part) series they include "Serializer" and "FileHandler" classes and show how to use them inside of the closures to lazy-load in data from an external file and work with it as serialized content.

tagged: tutorial closure lazyload file serialize view helper

Link:


Trending Topics: