<?php
class Counter
{
private static $count = 0; //定义一个静态属性
const TYPE_VERSION = 2; //定义一个常量
const TYPE_ACTIVED = 1; //定义一个常量
//
构造函数
function __construct()
{
self::$count++;
}
//
析构函数
function __destruct()
{
self::$count--;
}
//定义一个静态的方法
static function getCount()
{
return self::$count;
}
//翻译所有的const
public static function get_type_list()
{ //www.jb200.com
return array(
self::TYPE_VERSION => '版本',
self::TYPE_ACTIVED => '激活',
);
}
public static function getTypes()
{
return self::getConstants('TYPE_', __CLASS__, self::get_type_list());
}
public static function getConstants($token, $objectClass, $valOption = array(), $showOnlyOneLabel = false)
{
$tokenLen = strlen($token);
$reflection = new ReflectionClass($objectClass); //php built-in
$allConstants = $reflection->getConstants(); //constants as array
$tokenConstants = array();
foreach ($allConstants as $name => $val) {
if (substr($name, 0, $tokenLen) != $token)
continue;
if ($showOnlyOneLabel && 1 == count($valOption) && isset($valOption[$val])) {
$tokenConstants = $valOption[$val];
break;
}
$tokenConstants[$val] = isset($valOption[$val]) ? $valOption[$val] : $val;
}
return $tokenConstants;
}
}
print_r(Counter::getTypes());
//创建一个实例
$c = new Counter();
//执行打印
print(Counter::getCount() . "<br>n"); //使用直接输入类名来访问静态方法Counter::getCount
//打印类的版本
print("Version useed: " . Counter::TYPE_ACTIVED . "<br>n");
?>