Connecting Tech Pros Worldwide Forums | Help | Site Map

List all instances of a class?

L Robbins
Guest
 
Posts: n/a
#1: Jul 17 '05
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?

Oli Filth
Guest
 
Posts: n/a
#2: Jul 17 '05

re: List all instances of a class?


L Robbins said the following on 05/06/2005 12:06:[color=blue]
> 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?[/color]

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
Janwillem Borleffs
Guest
 
Posts: n/a
#3: Jul 17 '05

re: List all instances of a class?


L Robbins wrote:[color=blue]
> 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?[/color]

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



L Robbins
Guest
 
Posts: n/a
#4: Jul 17 '05

re: List all instances of a class?


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).
Tony Marston
Guest
 
Posts: n/a
#5: Jul 17 '05

re: List all instances of a class?


"L Robbins" <user@domain.invalid> wrote in message
news:d7vmci$l7n$1@news.freedom2surf.net...[color=blue]
> 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).[/color]

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

--
Tony Marston

http://www.tonymarston.net



R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#6: Jul 17 '05

re: List all instances of a class?


L Robbins wrote:[color=blue]
> 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[/color]
<snip>

http://www.php.net/get_defined_vars

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

Closed Thread