2012-12-17 64 views
0

是否有可能获得类定义静态数组的计数?例如:获取类静态数组的数量()

class Model_Example 
{ 

    const VALUE_1 = 1; 
    const VALUE_2 = 2; 
    const VALUE_3 = 3; 

    public static $value_array = array(
     self::VALUE_1 => 'boing', 
     self::VALUE_2 => 'boingboing', 
     self::VALUE_3 => 'boingboingboing', 
    ); 

    public function countit() 
    { 
     // count number 
     $total = count(self::$value_array); 
     echo ': '; 
     die($total); 
    } 
} 

目前调用countit()方法返回:

+1

做工精细这里http://codepad.org/XfFhv5FP –

回答

1

是的,它是可能的。上面代码中的问题是die()函数。如果参数die()是一个整数,它将被用作脚本的退出值,而不是打印到屏幕上。

变化countit()方法:

public function countit() 
{ 
    // count number 
    $total = count(self::$value_array); 
    echo ': ', $total; 
} 

你会发现更多的信息here

+0

我所用的模具作为dirty-调试:) – xylar

+0

@xylar hehe,不错的bug :) – hek2mgl