lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP: Singleton Pattern for Composed Objects
I usually use a variation of the method below in classes that include a composed object (e.g., a database object):

// method: load composed object
function _load_object()
{
// already loaded
if ( is_object($this->ComposedObject) ) return;

require_once($this->PATH['composed_object_class']);

// create composite object
$this->ComposedObject = new ComposedObject();
// add additional config steps here

return;
}
// END method


For an example, see the _load_phpmailer method in my CeoFramework class.

Labels: