By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,719 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,719 IT Pros & Developers. It's quick & easy.

Array of Instances Inside Constructor

P: n/a
Can anyone help me figure out how to make a class keep an array of all
of its instances? This was my first and obvious first shot...

class A
{
function A()
{
static $a = array() ;
$this->array =& $a ;

$this->array[] =& $this ;
}
}

But, if I make changes to the instance contents (not included above)
they are lost when I eventually try to use the array of references.
>From what I have read, it is not possible to use a reference to $this
like this because of when memory is actually created by PHP. (It's no
problem if I want to store an array of references AFTER the new
operator has returned the instance but I am trying to make it
transparent and put all of this inside the constructor where it should
be.)

Any tricks or workarounds are appreciated!

Feb 11 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
phpdevkit wrote:
Can anyone help me figure out how to make a class keep an array of all
of its instances? This was my first and obvious first shot...
....
>
But, if I make changes to the instance contents (not included above)
they are lost when I eventually try to use the array of references.
>>From what I have read, it is not possible to use a reference to $this
like this because of when memory is actually created by PHP. (It's no
problem if I want to store an array of references AFTER the new
operator has returned the instance but I am trying to make it
transparent and put all of this inside the constructor where it should
be.)

Any tricks or workarounds are appreciated!
Try this:

class Foo
{
public static $arr = [];
public function __construct() {
self::$arr[] = $this;
}
}

--
-- Fred of UrlBit.Us
-- http://UrlBit.Us - Bite those URLs down to size!
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Feb 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.