Get all objects of a certain class

I need to list objects that are an instance of a class using refrence

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

I need a solution to get all the objects that are an instance of the Foo class. Do you know how to do this?

+6
source share
2 answers

The solution to get all instances of a class is to keep records of the instance classes when they are created:

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

Now the globally accessible array Foo::$instances will contain all instances of this class. Your question was a little broad, so I can’t say for sure whether this is really what you are looking for. If not, it will hopefully help make it clearer what you are looking for.

+13
source

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

+2
source

Source: https://habr.com/ru/post/891442/


All Articles