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

I am currently using this method:

class Foo { private static $num_instances = 0; function __construct() { self::$num_instances++; } } 

which seems to work, but I wonder if there is a built-in way ....

+6
oop php
source share
3 answers

I would be surprised if he is. In my opinion, it will be overhead if it always counts the number of created copies.

0
source share

You can use xdebug using execution trace .

0
source share

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

That would not be the case, and I would rather do it with a static property.

0
source share

All Articles