计算创建PHP类的次数 [英] Count number of times a PHP class is created
本文介绍了计算创建PHP类的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个php类,可以为其创建多个实例.我想统计一下创建该对象的次数.
I've got a php class that I create several instances for. I'd like to get a count of how many times I've created that object.
<?php
class myObject {
//do stuff
}
$object1 = new myObject;
$object2 = new myObject;
$object3 = new myObject;
?>
是否可以找到我已经创建了3个myObjects的方法?
Is there a way to find that I've created 3 myObjects?
推荐答案
您可以创建构造函数时递增一个>被称为.
You can create a static counter and increment it each time your constructor is called.
<?php
class BaseClass {
public static $counter = 0;
function __construct() {
self::$counter++;
}
}
new BaseClass();
new BaseClass();
new BaseClass();
echo BaseClass::$counter;
?>
这篇关于计算创建PHP类的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文