Jean Pierre Daviau
2007-12-11 22:33:07 UTC
Bonjour a tous,
Je cherche à créer une classe qui me servirait à comptabiliser
des instances d'objets.. Cela existe-t-il déjà?
Voici un exemple fonctionnel mais qui manque de style et
peut-être d'exactitude.
---
class Counter{
//self:: to ccess static property
public static $count = 1;
public static $instanciated = array();
private $_ID_;
function __construct(){
$this->_ID_ = self::$count++;
self::$instanciated[$this->_ID_] = __CLASS__ . $this->_ID_;
}
function __clone(){
$this->_ID_ = self::$count++;
self::$instanciated[$this->_ID_] = __CLASS__ . $this->_ID_;
}
function __destruct(){
self::$instanciated[$this->_ID_] = NULL;
}
function get_ID_(){
return $this->_ID_;
}
function __toString(){
return (string)self::$instanciated[self::$count] . ' : ' .
$this->_ID_;
}
function getArray(){
return self::$instanciated;
}
function getClef(){
$key= array_search(__CLASS__ . $this->_ID_, self::$instanciated)
;
Return '---' . $key;
}
}
//test
$c1 = new Counter();
print($c1->get_ID_());
print(' $Counter1 = ' . $c1."\n");
$c2 = new Counter();
print($c2->get_ID_());
print(' $Counter2 = ' . $c1."\n");
$c3 = clone($c2);
print($c3->get_ID_());
print(' $Counter3 cloned2 = ' . $c1."\n");
print("------------\n");
//destroy one instanciated
print($c2->__destruct() . "\n");
echo '$Counter2 __destructed ' . $c2 . "\n";
print(' $Counter2 = ' . $c2->get_ID_() . "\n");
echo '-----------------' . "\n";
print($c1->get_ID_()."\n");
print(' $Counter1 = ' . $c1."\n");
print($c3->get_ID_()."\n");
print(' $Counter3 cloned2 = ' . $c3."\n");
echo '-----------------' . "\n";
print($c1->getClef());
//print($c2->getClef()); NULL
print($c3->getClef());
----------------
Je cherche à créer une classe qui me servirait à comptabiliser
des instances d'objets.. Cela existe-t-il déjà?
Voici un exemple fonctionnel mais qui manque de style et
peut-être d'exactitude.
---
class Counter{
//self:: to ccess static property
public static $count = 1;
public static $instanciated = array();
private $_ID_;
function __construct(){
$this->_ID_ = self::$count++;
self::$instanciated[$this->_ID_] = __CLASS__ . $this->_ID_;
}
function __clone(){
$this->_ID_ = self::$count++;
self::$instanciated[$this->_ID_] = __CLASS__ . $this->_ID_;
}
function __destruct(){
self::$instanciated[$this->_ID_] = NULL;
}
function get_ID_(){
return $this->_ID_;
}
function __toString(){
return (string)self::$instanciated[self::$count] . ' : ' .
$this->_ID_;
}
function getArray(){
return self::$instanciated;
}
function getClef(){
$key= array_search(__CLASS__ . $this->_ID_, self::$instanciated)
;
Return '---' . $key;
}
}
//test
$c1 = new Counter();
print($c1->get_ID_());
print(' $Counter1 = ' . $c1."\n");
$c2 = new Counter();
print($c2->get_ID_());
print(' $Counter2 = ' . $c1."\n");
$c3 = clone($c2);
print($c3->get_ID_());
print(' $Counter3 cloned2 = ' . $c1."\n");
print("------------\n");
//destroy one instanciated
print($c2->__destruct() . "\n");
echo '$Counter2 __destructed ' . $c2 . "\n";
print(' $Counter2 = ' . $c2->get_ID_() . "\n");
echo '-----------------' . "\n";
print($c1->get_ID_()."\n");
print(' $Counter1 = ' . $c1."\n");
print($c3->get_ID_()."\n");
print(' $Counter3 cloned2 = ' . $c3."\n");
echo '-----------------' . "\n";
print($c1->getClef());
//print($c2->getClef()); NULL
print($c3->getClef());
----------------
--
Merci de me lire
Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp
Merci de me lire
Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp