|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61953 A function for getting class instances
Submitted: 2012-05-05 18:36 UTC Modified: 2012-05-08 23:48 UTC
From: dev at pp3345 dot de Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: dev at pp3345 dot de
New email:
PHP Version: OS:


 [2012-05-05 18:36 UTC] dev at pp3345 dot de
I'd like to have a function that returns all instances of a given class with 
information about where it was instantiated and referenced. This could really 
help in debugging. A possible implementation could be

get_instances(string $class_name);


ReflectionClass::getInstances(string $class_name);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-08 23:48 UTC]
-Status: Open +Status: Not a bug
 [2012-05-08 23:48 UTC]
This information is not easily available from the engine and tracking it would be quite expensive. If you need it, you can do it using something like this: (untested)

$tracked_objects = array(); // Global

class TrackedClass {
    public function __construct() {
       $GLOBAL['tracked_objects'][spl_object_hash($this)] = array(
           'class' => get_class($this),
           'stack' => debug_backtrace()
    public function __destruct() {

Any class you want to track can than be extended from such a class (or use 5.4 and traits) andyou can inspect $tracked_objects.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 17 16:01:28 2024 UTC