Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

constant

1 view
Skip to first unread message

Jean Pierre Daviau

unread,
Dec 10, 2007, 8:33:24 AM12/10/07
to
Merry Christmass,

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


Jean Pierre Daviau

unread,
Dec 10, 2007, 4:04:14 PM12/10/07
to
> I would like to create a const defining every class I create.

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


ZeldorBlat

unread,
Dec 10, 2007, 5:51:28 PM12/10/07
to

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?

Jean Pierre Daviau

unread,
Dec 10, 2007, 10:48:53 PM12/10/07
to

"Jean Pierre Daviau" <On...@WasEno.ugh> a écrit dans le message de
news: oRh7j.5019$XD.1...@wagner.videotron.net...

>> I would like to create a const defining every class I create.
>
> and be noticed that a peticular instance has been destructed if
> so.

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];
}
}


Jean Pierre Daviau

unread,
Dec 11, 2007, 9:26:04 AM12/11/07
to
I wish someone will simplify this . . . even if it seems to work.

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());


Jean Pierre Daviau

unread,
Dec 12, 2007, 6:32:02 AM12/12/07
to
I will use a Singleton.

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


Csaba Gabor

unread,
Dec 12, 2007, 9:02:12 AM12/12/07
to

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

Jean Pierre Daviau

unread,
Dec 12, 2007, 6:38:03 PM12/12/07
to

"Csaba Gabor" <cs...@z6.com> a écrit dans le message de news:
b37f7$475fe6c3$506c17c2$31...@news.chello.at...
Yes.
Can I cast an object to a string?

Catchable fatal error: Object of class info2 could not be
converted to string

info2 Object
(
[var] => 0
[class] => info2
)


Jean Pierre Daviau

unread,
Dec 12, 2007, 11:15:46 PM12/12/07
to
The following works on the command line.

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
)

)
*/


Jean Pierre Daviau

unread,
Dec 15, 2007, 12:27:09 PM12/15/07
to
I gesss I will have to do something like this:


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);
}


Jean Pierre Daviau

unread,
Dec 20, 2007, 1:59:00 PM12/20/07
to
With object flush it seems to work.

<?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";

?>


0 new messages