Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

comptabiliser des instances d'objets

2 réponses
Avatar
Jean Pierre Daviau
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());


----------------

--
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

2 réponses

Avatar
Jean Pierre Daviau
IJe vais utiliser un Singleton.

Y a-t-il une fonction qui peut faire ceci?

$input = array();
$tmp = array();

$input[] = "a";
$input[] = "b";
$input[] = "c";
print_r($input);

$input[1] = NULL;

foreach($input as ($key=>$val != NULL)) {
if($val != NULL)
$tmp[] = $val;
}
$input = $tmp;
print_r($input);
Avatar
Jean Pierre Daviau
Re bonjour,

Pourquoi public static function popRegistre($val)
imprimet-elle le self::$leRegistre modifié alors que print_r
(Registre::getRegistre()); imprime le self::$leRegistre
original après que les modifications aient eu lieu?

La classe suivante fonctionne en ligne de commande.
------------------------------------
class Registre {

private static $leRegistre = array();
private static $tmp = array();

public static function getProperty($val) {
$num = array_search($val, self::$leRegistre);
return self::$leRegistre[$num];
}

public static function getRegistre() {

return self::$leRegistre;
}

public static function popRegistre($val) {
$i = 0;
$num = array_search($val, self::$leRegistre);
foreach(self::$leRegistre as $key=>$val) {
if($key == $num) continue;
self::$tmp[$i] = $val;
$i++;
}

self::$leRegistre = self::$tmp;
self::$tmp = null;

print_r(self::$leRegistre); //imprime le self::$leRegistre
modifié


}

public static function pushRegistre($value) {
self::$leRegistre[] = $value;
}


}


class info{
public $var = 0;
public $class = __CLASS__;

function __construct() {
Registre::pushRegistre($this);
}

function __destruct() {
$aa = Registre::popRegistre($this);
//print_r ($aa);
}
}
class info2{
public $var = 0;
public $class = __CLASS__;

function __construct() {
Registre::pushRegistre($this);
}

function __destruct() {
$aa = Registre::popRegistre($this);
//print_r ($aa);
}

}

$a = new info();
$b = new info2();
$a = NULL;


print_r(Registre::getProperty($a));
/*
info Object
(
[var] => 0
[class] => info
)

*/

print_r (Registre::getRegistre()); //n'imprime pas
self::$leRegistre modifié mais l'original
/*
Array
(
[0] => info Object
(
[var] => 0
[class] => info
)

[1] => info2 Object
(
[var] => 0
[class] => info2
)
)
*/

------------------------------------