Lazy Instantiation using is_a() and php5
<?php
class ObjectA
{
public function print_line($text)
{
print $text . "\n";
}
}
class ObjectB
{
public function ObjectA()
{
static $objecta;
if (!is_a('ObjectA', $objecta))
{
$objecta = new ObjectA;
}
return $objecta;
}
}
$obj = new ObjectB;
$obj->ObjectA()->print_line('testing, 1 2 3');
?>
In the above example, ObjectA is not instantiated until needed by ObjectB. Then ObjectB can continually use it's creation as needed without reinstantiating it.
There are other ways, but I like this one :-)