Req #61953 [Opn->Nab]: A function for getting class instances

From: Date: Tue, 08 May 2012 23:48:07 +0000
Subject: Req #61953 [Opn->Nab]: A function for getting class instances
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170322@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61953&edit=1

 ID:                 61953
 Updated by:         johannes@php.net
 Reported by:        dev at pp3345 dot de
 Summary:            A function for getting class instances
-Status:             Open
+Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Class/Object related
 PHP Version:        5.4.2
 Block user comment: N
 Private report:     N

 New Comment:

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() {
       unset($GLOBAL['tracked_objects'][spl_object_hash($this)]);
    }
}

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.


Previous Comments:
------------------------------------------------------------------------
[2012-05-05 18:36:05] dev at pp3345 dot de

Description:
------------
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);

or

ReflectionClass::getInstances(string $class_name);



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61953&edit=1


Thread (2 messages)

« previous php.bugs (#170322) next »