Re: how to count objects instances
| From: | Curt Zirzow | Date: | Wed, 25 Aug 2004 05:43:16 +0000 |
| Subject: | Re: how to count objects instances | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194651@lists.php.net to get a copy of this message | ||
* Thus wrote tyler@carbon.homelinux.com:
> Thanks,
> But im using php 4.3.
Then you'll have to resort to some very unstandard methods:
class foo {
var $instance_manager;
function getInstanceNum() {
return $this->instance_manager->instancesOf(__CLASS__);
}
function getInstanceName() {
return __CLASS__;
}
}
class Factory {
var $instances;
function &create($name) {
$this->instances[$name]++; // keep track of instances.
$new = new $name(); // create object
$new->instance_manager = &this; // tell instance about me
return &$new; // return value.
}
function destroy(&$obj) {
$this->instances[$this->getInstanceName()]--;
unset($obj);
}
function instancesOf($name) {
return $this->instance[$name];
}
}
$Factory = new Factory();
$o = &$Factory->Crate('foo');
echo $o->getIntanceNum();
$Factory->destroy($o);
btw, totally untested.
Curt
--
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about. No, sir. Our model is the trapezoid!