Monday, February 21, 2011

Is there a way to find out how many times a class has been instantiated in php?

I'm currently using this method:

class Foo {
    private static $num_instances = 0;

    function __construct() {
        self::$num_instances++;
    }
}

which seems to work, but I'm wondering if there's a built in way....

From stackoverflow
  • I would be surprised if there is one..
    In my opinion it would be an overhead, if it is always counting the amount of created instances.

  • You could use xdebug using the execution trace.

  • You can always check $GLOBALS and count the number of class instantiations.

    It wouldn't be pretty, and I would prefer to do it with a static property.

0 comments:

Post a Comment