class test {    static $mystr = 'test';    function get() {
        echo test::$mystr;
    }    function get2() {
        echo self::$mystr;
    }}$mytest = new test();$mytest->get();
$mytest->get2();都能输出test,那么静态变量在类内使用类名调用跟使用self调用有区别吗?

解决方案 »

  1.   

    你看看这个
    class test {
      static $mystr = 'test';
    }
    class test1 {
      static $mystr = 'test1';
      function get() {
        echo test::$mystr;
      }
      function get2() {
        echo self::$mystr;
      }
    }
    $mytest = new test1();
    $mytest->get();
    $mytest->get2();
      

  2.   

    静态变量在类外声明合法吗?
    像这样
    <?php
    function test() {
        static $a = 1;
        echo $a++;
    }
      

  3.   

    class test {  static $mystr = 'test';
      function get() {
      echo test::$mystr;
      }
      function get2() {
      //self为自身
      echo self::$mystr;
      }
    }
    建议本类都是用self,比如以后你的类重新命名了成test_new了,self没有问题,而test就不能用了