I would like to create a const defining every class I create.
class Counter{
const ID;
function __construct(){
$this->ID = get_class($this) . ' : ' . rand();
----------
--
Thanks for your attention.
Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp
and be noticed that a peticular instance has been destructed if
so.
class Counter{
private static $instancieted = 0;
function __construct(){
self::$instancieted++;
$IDE = get_class($this) . ' : ' . rand();
define('_IDE_', $IDE);
}
function __destruct(){
self::$instancieted--;
}
function __toString(){
return _IDE_ . ' : instancieted # : ' . self::$instancieted;
}
If every instance of the class has a different value for the constant
then it really isn't a class constant -- which is why you can't define
class constants that way.
Why not use an instance variable instead?
plan B
class Counter{
private static $count = 0;
private static $instancieted = array();
private $_ID_ = 0;
function __construct(){
$this->_ID_ = self::$count++;
self::$instancieted[$this->_ID_] = (string)(__CLASS__ . ' : ' .
$this->_ID_);
}
function __clone(){
$this->_ID_ = self::$count++;
self::$instancieted[$this->_ID_] = (string)(__CLASS__ . ' : ' .
$this->_ID_);
}
function __destruct(){
self::$instancieted[$this->_ID_] = (string)(__CLASS__ . ' : ' .
$this->_ID_);
$this->_ID_ = NULL;
self::$count--;
}
function getCount(){
return self::$count;
}
function __toString(){
return (string)$this->instancieted[self::$count];
}
}
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;
}
}
//create one instanciated
$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());
Is ther a function to do this?
$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);
I haven't been following along with what you are doing,
but your example could be much shorter with:
$input = str_split("abc");
print_r($input);
print "<br>";
$input[1] = NULL;
$input = array_merge(array_diff($input, array(null)));
print_r($input);
Csaba Gabor from Vienna
Catchable fatal error: Object of class info2 could not be
converted to string
info2 Object
(
[var] => 0
[class] => info2
)
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); //print correctly the changed
self::$leRegistre
}
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)); //dont print the changed
self::$leRegistre
/*
info Object
(
[var] => 0
[class] => info
)
*/
print_r (Registre::getRegistre()); //dont print the changed
self::$leRegistre
/*
Array
(
[0] => info Object
(
[var] => 0
[class] => info
)
[1] => info2 Object
(
[var] => 0
[class] => info2
)
)
*/
public $_INumber;
public $members;
public $methods;
function __construct() {
$this->members = "\n\t[variables]: " .
Registre::objectToText(get_class_vars(__CLASS__));
$this->methods = "\n\t[méthodes]: " .
Registre::objectToText(get_class_methods(__CLASS__));
$this->_INumber = "\n\t[_INumber]: " . __CLASS__ . ' #' .
rand();
Registre::pushRegistre($this->_INumber . $this->members .
$this->methods);
}
<?php
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) {
ob_start();
$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;
ob_flush();
}
public static function pushRegistre($value) {
self::$leRegistre[] = $value;
print_r(self::$leRegistre);
}
}
class info1{
public $_INumber;
public $members;
public $methods;
function __construct() {
Registre::pushRegistre(__CLASS__ . ' #' . rand());
}
function __destruct() {
Registre::popRegistre($this->unique_ID);
}
}
$a = new info1();
$b = new info1();
$a = null;
echo "-----\n";
print_r(Registre::getRegistre()) ;
echo "-----\n";
?>