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

List all instances of a class?

P: n/a
I am a newcomer to PHP and am writing a script which dynamically creates
instances of a class with names like $object1, $object2, etc.. I now
want my script to be able to list all of the objects which are instances
of a specific class -- but I haven't been able to find any code which
does this in the online PHP references I've consulted. Given that the
names of the objects are predictable, it would be possible for me to
write some code which looped sequentially through the object names
$object1, $object2, etc., until it reached an object name which didn't
exist. However, I was expecting there to be a tidier, easier way of
doing the same thing with a command that says simply "List all instances
of class X."

Could you please tell me how to do this, or why it is not possible?
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
L Robbins said the following on 05/06/2005 12:06:
I am a newcomer to PHP and am writing a script which dynamically creates
instances of a class with names like $object1, $object2, etc.. I now
want my script to be able to list all of the objects which are instances
of a specific class -- but I haven't been able to find any code which
does this in the online PHP references I've consulted. Given that the
names of the objects are predictable, it would be possible for me to
write some code which looped sequentially through the object names
$object1, $object2, etc., until it reached an object name which didn't
exist. However, I was expecting there to be a tidier, easier way of
doing the same thing with a command that says simply "List all instances
of class X."

Could you please tell me how to do this, or why it is not possible?


If you've got a load of sequentially numbered variables like $object1,
$object2, $object3 and you want to do things like loop through them, it
makes far more sense to use an array than discrete variables, i.e.
$object[0], $object[1], $object[2] (numbering usually starts from 0). Do
that and there's a whole host of PHP functions you can use that are
designed to manipulate arrays (see
http://www.php.net/manual/ref.array.php). This includes count(), which
tells you how many things there are in the array.

If you're not familiar with arrays, see
http://www.php.net/manual/language.types.array.php

--
Oli
Jul 17 '05 #2

P: n/a
L Robbins wrote:
Given that the names of the objects are predictable, it
would be possible for me to write some code which looped sequentially
through the object names $object1, $object2, etc., until it reached
an object name which didn't exist. However, I was expecting there to
be a tidier, easier way of doing the same thing with a command that
says simply "List all instances of class X."

Could you please tell me how to do this, or why it is not possible?


There is no function that returns all the instances of a class. If you want
to keep track of them, you could store them in an array or loop through the
$GLOBALS array and test each key with is_object() and instanceof (or is_a()
when not using PHP5).

When using PHP5, you could also keep track of all created instances as
follows:

<?php

class Foo {
private static $instance_count = 0;
public static function getInstanceCount() {
return self::$instance_count;
}

function __construct() {
self::$instance_count++;
}

function __destruct() {
self::$instance_count--;
}
}

$foo = new Foo;
print Foo::getInstanceCount(); // 1

unset($foo);
print Foo::getInstanceCount(); // 0

?>
JW

Jul 17 '05 #3

P: n/a
Thanks very much, Janwillem and Oli. In my neophyte's enthusiasm for
object-oriented PHP, I was trying to instantiate objects wherever I saw
the vaguest possibility for them. I have now reworked my code with a
wiser, more jaded eye (though I still think it would be nice if there
were an in-built way to list all instances of a class).
Jul 17 '05 #4

P: n/a
"L Robbins" <us**@domain.invalid> wrote in message
news:d7**********@news.freedom2surf.net...
Thanks very much, Janwillem and Oli. In my neophyte's enthusiasm for
object-oriented PHP, I was trying to instantiate objects wherever I saw
the vaguest possibility for them. I have now reworked my code with a
wiser, more jaded eye (though I still think it would be nice if there were
an in-built way to list all instances of a class).


Do other OO languages provide this facility? If not, then why do you expect
PHP to?

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #5

P: n/a
L Robbins wrote:
I am a newcomer to PHP and am writing a script which dynamically creates
instances of a class with names like $object1, $object2, etc.. I now
want my script to be able to list all of the objects which are instances
of a specific class

<snip>

http://www.php.net/get_defined_vars

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.