2012-12-17 62 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