O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.1. Problem and Solution

When an object is instantiated, it is technically a complete object. Some objects can be used in this capacity and are ready to propagate throughout the code flow. However, other objects are more complex in nature. They may require additional public methods to execute to be considered "complete" and available for the rest of the application.

It is important to understand what the word complex means in this context. Generally, complexity is the actual logic contained within methods of a class. However, when I refer to a complex object in connection to object instantiation, it means the level of steps that are required to create that complete object. The actual logical steps in each of the executed methods have no bearing on whether these classes are complex.

When creating complex objects, a common architecture decision revolves around the creation of the constructor. Some programmers think that any constructor should execute the proper logic to create the whole object. Others recognize that it makes sense to break up some of that logic into additional methods. Constructors designed in that fashion are basically a list of methods to call on instantiation. Neither of these solutions are very flexible. In fact, they are fundamentally wrong solutions.

It may be necessary to construct an object based on the results of a set of business logic. In this particular example, the base business decision rules have already been written and tested. Because of the results, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required