note 57584 modified in function.is-a by felipe

From: Date: Mon, 10 Dec 2007 22:51:27 +0000
Subject: note 57584 modified in function.is-a by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-133335@lists.php.net to get a copy of this message
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 :-) --was-- 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 :-) http://php.net/manual/en/function.is-a.php

« previous php.notes (#133335) next »