Get All Instances of a Class in PHP

Get all instances of a class in PHP

If you derive all your objects from a TrackableObject class, this class could be set up to handle such things (just be sure you call parent::__construct() and parent::__destruct() when overloading those in subclasses.

class TrackableObject
{
protected static $_instances = array();

public function __construct()
{
self::$_instances[] = $this;
}

public function __destruct()
{
unset(self::$_instances[array_search($this, self::$_instances, true)]);
}

/**
* @param $includeSubclasses Optionally include subclasses in returned set
* @returns array array of objects
*/
public static function getInstances($includeSubclasses = false)
{
$return = array();
foreach(self::$_instances as $instance) {
if ($instance instanceof get_class($this)) {
if ($includeSubclasses || (get_class($instance) === get_class($this)) {
$return[] = $instance;
}
}
}
return $return;
}
}

The major issue with this is that no object would be automatically picked up by garbage collection (as a reference to it still exists within TrackableObject::$_instances), so __destruct() would need to be called manually to destroy said object. (Circular Reference Garbage Collection was added in PHP 5.3 and may present additional garbage collection opportunities)

Get all objects of a particular class

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.

How to get instance of a specific class in PHP?

What you have described is essentially the singleton pattern. Please see this question for good reasons why you might not want to do this.

If you really want to do it, you could implement something like this:

class a {
public static $instance;
public function __construct() {
self::$instance = $this;
}

public static function get() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}

$a = a::get();

Loop through each instance of a class in php

Base on your code, I would approach it something like this:

// instantiate projects array like this
$projects = array(
new Project("Title1", "Summary", "Tools"),
new Project("Title2", "Summary", "Tools"),
new Project("Title3", "Summary", "Tools"),
new Project("Title3", "Summary", "Tools"),
);

// then iterate each $projects and then call the displayProjectCard()
foreach ($projects as $project) {
$project->displayProjectCard();
}

Get all instances of a class in PHP

If you derive all your objects from a TrackableObject class, this class could be set up to handle such things (just be sure you call parent::__construct() and parent::__destruct() when overloading those in subclasses.

class TrackableObject
{
protected static $_instances = array();

public function __construct()
{
self::$_instances[] = $this;
}

public function __destruct()
{
unset(self::$_instances[array_search($this, self::$_instances, true)]);
}

/**
* @param $includeSubclasses Optionally include subclasses in returned set
* @returns array array of objects
*/
public static function getInstances($includeSubclasses = false)
{
$return = array();
foreach(self::$_instances as $instance) {
if ($instance instanceof get_class($this)) {
if ($includeSubclasses || (get_class($instance) === get_class($this)) {
$return[] = $instance;
}
}
}
return $return;
}
}

The major issue with this is that no object would be automatically picked up by garbage collection (as a reference to it still exists within TrackableObject::$_instances), so __destruct() would need to be called manually to destroy said object. (Circular Reference Garbage Collection was added in PHP 5.3 and may present additional garbage collection opportunities)

How can I get a list of instances of a class

No, you can't do it. You can build factory class for creation objects and store them in static array.



Related Topics



Leave a reply



Submit