【问题标题】:Getting count() of class static array获取类静态数组的count()
【发布时间】:2012-12-17 22:53:10
【问题描述】:

是否可以获得类定义的静态数组的计数?例如:

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()方法返回:

【问题讨论】:

标签: php arrays static-members


【解决方案1】:

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

将 countit() 方法更改为:

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

你会发现更多信息here

【讨论】:

  • 我把骰子用作脏调试:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2010-12-02
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多