获取特定类的所有对象 [英] Get all objects of a particular class

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

问题描述

我必须通过refrence列出作为类的实例的对象

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;

我需要一个解决方案来获取所有对象的Foo类
的实例你知道吗?如何做到这一点?

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

推荐答案

获取类的所有实例的解决方案是在创建实例化类时记录实例化类:

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;
  }
}

现在全局可访问数组 Foo :: $ instances 将包含该类的所有实例。你的问题有点宽,所以我不能确切地说,如果这是你要找的。如果没有,它希望有助于使它更清楚你要找什么。

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.

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

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