Use Static member to track instance number in PHP
Description
The following code shows how to use Static member to track instance number.
Example
/* www .j av a2 s . c o m*/
<?php
class Counter
{
private static $count = 0;
const VERSION = 2.0;
function __construct()
{
self::$count++;
}
function __destruct()
{
self::$count--;
}
static function getCount()
{
return self::$count;
}
}
//create one instance
$c = new Counter();
//print 1
print(Counter::getCount() . "<br>\n");
//print the version of the class
print("Version used: " . Counter::VERSION . "<br>\n");
?>
The code above generates the following result.