PHP成员函数中竟然也能用static声明变量

又发现一个PHP Feature,PHP成员函数中竟然也能用static声明变量。怎么想怎么奇怪,你都创建新实例了,怎么static还是有效?不过有时候还蛮有用的。

class static_var {
    function __construct($name) {
        $this->name = $name;
    }
    function output() {
        static $counter;
        $counter++;
        echo $counter.'|'.$this->name.'|';
    }
}
$t1 = new static_var('take 1');
$t1->output();
$t2 = new static_var('take 2');
$t2->output();

One comment to PHP成员函数中竟然也能用static声明变量

  1. liwei says:

    你好,程序猿 :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>