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
How to Iterate Over Non-English File Names in PHP
How to Use PHP Reserved Names for My Functions and Classes
How to Throttle My Site's API Users
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
How to Get the Unicode Value of a Character or Vise Versa with PHP
Download Files from Server PHP
Can Someone Explain the /E Regex Modifier
How to Correct This Illegal String Offset
Set Precision for a Float Number in PHP
Avoiding Form Resubmit in PHP When Pressing F5
PHP Keep Checkbox Checked After Submitting Form
Composer Killed While Updating
Disabling Strict Standards in PHP 5.4
What Is the Equivalent of JavaScript's Encodeuricomponent in PHP
Casperjs Passing Data Back to PHP