Heim  >  Fragen und Antworten  >  Hauptteil

php - 为什么这会报错

<?php
class aa{
   static public  $zz = 8;
   public function a1(){
        $a=1;
        $this->a2();
        return $a;

    }
    
    public function a2(){
        $this->zz +=1;
    }
}

$k = new aa();
$k->a1();
$k->a2();
 
echo $k->zz;
echo '<br>';
echo $k->a1();
die();

Strict Standards: Accessing static property aa::$zz as non static in D:WWWclient.php on line 13

Notice: Undefined property: aa::$zz in D:WWWclient.php on line 13

Strict Standards: Accessing static property aa::$zz as non static in D:WWWclient.php on line 27
2

Strict Standards: Accessing static property bb::$zz as non static in D:WWWclient.php on line 13

Notice: Undefined property: bb::$zz in D:WWWclient.php on line 13
1

漂亮男人漂亮男人2687 Tage vor490

Antworte allen(3)Ich werde antworten

  • phpcn_u1582

    phpcn_u15822017-05-16 13:15:40

    静态成员变量只能由静态方法访问,静态方法只能访问静态成员

    访问静态成员方式:

    类内调用
    self::$zz += 1;
    
    类外调用
    aa::$zz;

    Antwort
    0
  • 阿神

    阿神2017-05-16 13:15:40

    你申明了$zz是static静态变量,然后你用$this去调用,能不出问题?

    Antwort
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:15:40

    静态成员变量用静态的方式调用

    Antwort
    0
  • StornierenAntwort