Omid
Omid

Reputation: 4705

Get all objects of a particular class

i have to list objects that are instance of a class by refrence

class Foo {}
class Foo1 {}
$obj1 = new Foo;
$obj2 = new Foo;
$obj32 = new Foo1;

i need a solution to get all objects that are instance of Foo class do you know how to do that ?

Upvotes: 6

Views: 5542

Answers (2)

hakre
hakre

Reputation: 197659

A solution to get all instances of a class is to keep records of instantiated classes when you create them:

class Foo
{
  static $instances=array();
  public function __construct() {
    Foo::$instances[] = $this;
  }
}

Now the globally accessible array Foo::$instances will contain all instances of that class. Your question was a bit broad so I can not exactly say if this is what you're looking for. If not, it hopefully helps to make it more clear what you're looking for.

Upvotes: 14

Brian
Brian

Reputation: 8616

See this answer Get all instances of a class in PHP has worked for me to do this in the past.

Upvotes: 2

Related Questions