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

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


Terry Chay's Blog:
Autoloading and Lazy Loading
Apr 27, 2009 @ 11:14:26

In this recent post to his blog Terry Chay points out one way he (and Tagged) used to help prevent things like "unknown class" errors in their code - lazy loading.

It’s not that Andrei is wrong in his admonition. Far from it! For reasons that I don’t quite care to know, there are caching and lookup optimizations that APC cannot do when it has to switch context to run __autoload.

They did see a performance boost from the code rewrite it took to make this happen but, since not everyone can take the time to optimize their code like this, he also suggests another way (as written up by Brian Shire). It uses two settings for APC in your php.ini file and the latest versions of PHP 5.3 & APC along with this patch to make them work.

tagged: autoload lazy load apc cache performance increase


Padraic Brady's Blog:
Unit Testing: Multiple Assertions And Lazy/Shallow Testing Are Evil
Feb 13, 2009 @ 09:31:33

In a new post today Padraic Brady takes a look at unit testing and two of the bad practices that can develop over time when writing your tests - overloading tests with assertions and lazy/shallow tests.

In short, every test you write requires that you setup the test environment, create a scenario for possible failure, add an assertion, and then ensure the source code makes that assertion pass. This requires code - sometimes a lot of code. So adding multiple assertions to each test minimizes the work needed to write tests, since using multiple assertions takes advantage of existing code to avoid writing new stuff to clutter your test classes. It can also help to tackle multiple but related results in the same test.

He gives brief examples of both instances and some of the problems associated with them. Multiple assertions, while nice for fine tuning the results of the testing, can also confuse since, if one assertion fails, the whole test fails too. The other end of the spectrum is a problem too - writing tests that don't do enough to really check the information passed in. These shallow tests can lead to issues down the line if unanticipated data might come along.

Really, its all about finding that "sweet spot" in developing tests - not too much, not too little - and keeping it simple while not missing out on assertions that need to be made.

tagged: unittest phpunit lazy shallow multiple assertion example