获取PHP中的类的所有实例 [英] Get all instances of a class in PHP

查看:123
本文介绍了获取PHP中的类的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得某个类的对象的所有实例。



例如:

  class Foo {
}

$ a = new Foo();
$ b = new Foo();

$ instances = get_instances_of_class('Foo');

$ instances 应为 array($ a,$ b)数组($ b,$ a) >

A plus是如果函数返回具有所请求类的超类的实例,但这不是必需的。



我可以想到的一种方法是使用一个静态类成员变量,它保存一个实例数组。在类的构造函数和析构函数中,我将从数组中添加或删除 $ this

解决方案

如果你从所有的对象中派生所有的对象,那么这是非常麻烦和容易出错的。一个TrackableObject类,这个类可以设置为处理这样​​的事情(只要确保你调用 parent :: __ construct() parent :: __ destruct ()

  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可选地,在返回集中包含子类
* @returns对象的数组数组
* /
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;
}
}

这个的主要问题是没有对象会被垃圾回收作为它的引用仍然存在于 TrackableObject :: $ _ instances )中,因此需要调用 __ destruct()手动破坏所述对象(在PHP 5.3中添加了循环引用垃圾回收,可能会提供额外的垃圾回收机会)


I would like to get all the instances of an object of a certain class.

For example:

class Foo {
}

$a = new Foo();
$b = new Foo();

$instances = get_instances_of_class('Foo');

$instances should be either array($a, $b) or array($b, $a) (order does not matter).

A plus is if the function would return instances which have a superclass of the requested class, though this isn't necessary.

One method I can think of is using a static class member variable which holds an array of instances. In the class's constructor and destructor, I would add or remove $this from the array. This is rather troublesome and error-prone if I have to do it on many classes.

解决方案

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)

这篇关于获取PHP中的类的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆