473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

List all instances of a class?

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
5 5715
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Crawley | last post by:
Im trying to create a list of lists and for some reason its not working: class Tile: _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None, 'sw':None, 'w':None, 'nw':None } def...
2
by: John Wohlbier | last post by:
Hi, I have a basic programming question regarding classes in python. I want to have a list of "primaryClass" instances, and in each instance of primaryClass I would like a list of "subClass"...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
5
by: majm | last post by:
I'm trying to implement strongly typed lists in the 2.0 framework. I'm using VS2005 beta 2. So far, System.Collections.Generic.List appears to be the ideal solution. However, the...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
3
by: manstey | last post by:
how do I detect a change in a list of class instances? from copy import deepcopy class CaListOfObj(list): """ subclass of list """ def __init__(self, *args, **kwargs): list.__init__(self,...
8
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to get a list of SQL Server Instances thru a VB.NET application. I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe from...
6
by: David C | last post by:
In my business layer, I have Person, and Patient which derives from Person. //base class for all single classes public class BaseItem{} public class Person:BaseItem{} public class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.