List all instances of a class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • L Robbins

    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?
  • Oli Filth

    #2
    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


    --
    Oli

    Comment

    • Janwillem Borleffs

      #3
      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 getInstanceCoun t() {
      return self::$instance _count;
      }

      function __construct() {
      self::$instance _count++;
      }

      function __destruct() {
      self::$instance _count--;
      }
      }

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

      unset($foo);
      print Foo::getInstanc eCount(); // 0

      ?>


      JW



      Comment

      • L Robbins

        #4
        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).

        Comment

        • Tony Marston

          #5
          Re: List all instances of a class?

          "L Robbins" <user@domain.in valid> wrote in message
          news:d7vmci$l7n $1@news.freedom 2surf.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

          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




          Comment

          • R. Rajesh Jeba Anbiah

            #6
            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>



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

            Comment

            Working...