|
Posted by Oli Filth on 06/05/05 15:09
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
[Back to original message]
|