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

Nikola Posa:
Lazy loading services using Zend Service Manager
Jul 16, 2018 @ 11:41:34

On his site Nikola Posa has a tutorial showing how to lazy load services with Zend Manager, a component of the Zend Framework. In this case, the "services" being loaded are in a dependency injection container.

Any more complex application includes a big dependency injection tree of services, some of which can have a more complicated creation logic. If the service is injected as a dependency, but not necessarily used at every execution, you may want to lazily initialize that service until it is really needed.

In those situations, you may be tempted to inject the entire Dependency Injection Container instead, and lazy-load that resource-hungry service. I find that to be an anti-pattern, and I explained my views in a blog post written some time ago.

He offers a better solution in the form of the proxy design pattern, making it possible to decouple the services from the dependency injection container. He provides examples of using this pattern along with the Zend Service Manager functionality to create the factories and the services configuration.

tagged: lazy load services zend servicemanager tutorial proxy designpattern

Link: https://blog.nikolaposa.in.rs/2018/07/14/lazy-loading-services-using-zf-service-manager/

Andreas Möller:
Makefile for lazy developers
Jan 26, 2018 @ 11:45:18

In a post to his site Andreas Möller shares a tool that he uses to get an application up and running quickly, providing a makefile for lazy developers.

Whatever the size of the software project, I believe in, subscribe to, and promote Continuous Integration. Personally, I rely on Travis CI as an automated build system. Regardless of whether an automated build system can be set up and used for a project or not, I prefer to be able to run build steps locally. This prevents stress testing the automated build system and taking away resources from other developers. Also, it gives me more confidence before committing and pushing changes upstream.

[...] For a couple of years now I have been using make, after having been introduced to it when working on a project in 2014. While it has its limitations, it’s short and simple, and most of all, it get’s the job done.

He then talks about the repository he's created to get up and running quickly that creates a simple Makefile to define several make commands and shortcuts for some common tasks. The make it task is the most used, executing all of the other tasks to ensure that all tests pass, the code is well-structured and generates a coverage report to ensure as much of the code is covered by tests as it should be.

tagged: makefile make tutorial repository common task lazy

Link: https://localheinz.com/blog/2018/01/24/makefile-for-lazy-developers/

QaFoo Blog:
Refactoring Singleton Usage to get Testable Code
Jul 11, 2017 @ 12:22:07

The QaFoo.com blog has a new post sharing a helpful hint on refactoring singletons to make them more testable. Singletons and notoriously difficult to test due to how it can potentially return an unexpected version of an object.

So your code base is littered with singletons and using them? Don't worry, you can start refactoring them out of your code base class by class and introduce increased testability at every step. This strategy is very simple to implement and the probability of breaking your code is very low, especially when you are becoming more experienced with this technique.

They give an example of a service class that uses a singleton to get an instance of the Solarium_Client class via a static method call. They show how to refactor this out into a separate method and then use the "lazy initialization" pattern to only use the singleton if the property isn't already defined. This then allows you to use a setter to inject your own client during testing (a mock most likely).

tagged: refactor testing unittest mock singleton property lazy initialization

Link: https://qafoo.com/blog/107_refactoring_singletons_testability.html

Alejandro Celaya:
Using ServiceManager 3 lazy services to improve your PHP application performance
Jun 13, 2016 @ 10:20:18

Alejandro Celaya has posted a tutorial to his site showing you how to use ServiceManager 3 to improve performance in your PHP-based application. The ServiceManager is a piece of the Zend Framework.

Performance is an important subject when a project grows. There are some good practices that make projects more maintainable, like dependency injection, however, creating all the objects at the beginning of the request could reduce the application performance. If some of the created objects are not finally used, we have wasted CPU time and memory for no reason.

If we used proxies for every expensive dependency, the previous problem would be solved. We can still inject the dependency, but it will be wrapped by the proxy, which will create the object itself once we need it, or never, if it is not finally used. This is the principle behind lazy services. The ServiceManager makes use of the ocramius/proxy-manager package to create proxies on the fly for all the services configured as lazy.

He talks about the lazy_services functionality the ServiceManager provides and gives an example of it in use defining a database (PDO) connection. He talks some about how it works behind the scenes and how no code change is required to use this new configuration.

tagged: performance application servicemanager3 lazy services example tutorial zendframework

Link: http://blog.alejandrocelaya.com/2016/06/12/using-service-manager-3-lazy-services-to-improve-your-php-application-performance/

Marc Morera:
Lazy Commands in Symfony
May 08, 2015 @ 08:13:22

In the latest post to his site Marc Morera about the use of "lazy services" with Symfony2. In his examples, he uses a command line application to illustrate his point, but it could apply elsewhere as well.

Since Symfony version 2.4 you can define your controllers and commands as services. This is so useful as long as you need to treat your classes as much decoupled as possible. [...] When we define as lazy a service, this is not instanced when is injected, but only when is accessed. [...] The point here is to define our service intended to work with the model as lazy.

He shows how to implement this kind of "lazy" handling in a command, registering the commands into the services but not creating the instances of them until they're used. He includes some example code showing how this is set up and offers a few tips on the implementation and common issues to think about.

tagged: symfony2 command lazy service register tutorial

Link: http://mmoreram.com/blog/2015/05/08/lazy-commands-in-symfony/

Web Species Blog:
Lazy evaluation with PHP
Jun 01, 2011 @ 08:41:01

Juozas Kaziukenas has a new post to his Web Species blog about using "lazy evaluation" in PHP - loading the resources you need for execution and evaluation only as you need them, not all up front.

Recently I needed to process a huge array of data and because of PHP's somewhat inefficient variables and especially arrays that was resulting in "out of memory" errors. However, I couldn't use any other tools than PHP so was forced to come up with a solution implementation in it. Here is how I solved it using principles from functional languages.

He gives an example using Haskell to generate a Fibonacci sequence using its built-in lazy evaluation abilities. Unfortunately, PHP doesn't have such a thing built in, so he tries the next best thing - Iterators. He caries the idea over to the database side too, recommending fetch() in a loop over fetchAll() and some effective joins.

tagged: lazy evaluation haskell functional iterator


Lazy and Eager Loading in PHP 5
Sep 11, 2009 @ 12:49:30

On DevShed today there's the start of a new series looking at design patterns in PHP applications. In this first part of the series, they look at lazy and eager loading.

In the case of PHP 5, which is better suited for developing web-based programs, there are two complementary design patterns that can be of great help in building faster and more reliable applications. Of course, as this article’s title suggests, I’m talking about the lazy and eager loading patterns. Despite their rather funny names, they are pretty easy to implement in PHP 5-controlled environments.

To illustrate, they've created a sample class that uses a few class properties and a __toString method to return the values.

tagged: lazy eager loading tutorial designpattern


Why PHP Frameworks Matter
Aug 12, 2009 @ 11:09:50

From BuildInternet.com today there's a new article from Jason Gilmore looking at PHP frameworks and why they matter to both the developers and the community at large.

Having been a PHP programmer for more than a decade, I’ve come to realize that many of my fellow PHP programmers seem to have a misplaced notion of what being "lazy" is really all about. [...] Suddenly the Web developer was facing so much more than validating user input and connecting to a database [and] the need to be a lazy programmer was more important than ever.

He covers a lot of the basics of frameworks with a focus on the Zend Framework (because of familiarity) - configuration, data validation tools, database connections, display helpers and the inclusion of third-party plugins.

tagged: framework important lazy overview


Giorgio Sironi's Blog:
Doctrine 2 now has lazy loading
Aug 07, 2009 @ 11:07:37

As is mentioned in this new post to Giorgio Sironi's blog, the latest version of Doctrine now includes lazy loading functionality.

Lazy loading is the capability of performing a expensive operation on demand, only when it reveals necessary from a client request. [...] In Doctrine 2 I made some architectural choices implementing the proxy approach, which substitute a subclassing object to every association which is not eager-loaded.

There's two main places you can see these differences - in the one/many to one associations dynamic proxies and collection interface.

tagged: doctrine lazy load relationships


Giorgio Sironi's Blog:
Lazy loading of objects from database
Jul 21, 2009 @ 12:48:54

In recent post to his blog Giorgio Sironi takes a look at a handy feature that PHP makes it relatively simple to work with objects in your applications - lazy loading.

What is lazy loading? A practice in object/relational mapping which simulate the presence of the whole object graph in memory. There is some techniques for produce this illusion that I explain here. [...] The main problem is that we cannot load all the object graph, because it will not fit in the memory of the server and it will take much time to build, depending on the size of the database. Nor we can load an arbitrary subset of it.

His solution is to use the proxy design pattern to create an interface allowing only the first level of information to be loaded by default. After that, each of the sub-levels have what they need to load themselves and the data that might be needed inside.

tagged: tutorial database object load lazy