我想測試static方法,報錯
代碼如下:
<?php
class Car {
? ? public $speed = 0;
? ? //增加speedUp方法,使speed加10
? ? public static function speedUp(){
? ? ? ? $speed1=0;
? ? ? ? $this ->speed = $speed1+10;
? ? ? ? return $speed1;
? ? }
}
echo Car::speedUp();
報錯如下:Fatal error: Using $this when not in object context in index.php on line 7
提示說不能再當前方法下使用$this指針,請問怎么回事。
2019-06-25
class Car {
? ?public $speed = 0;
? ? //增加speedUp方法,使speed加10
? ? public static function speedUp(){
? ? ? ? $speed1=0;
? ? ? ? $speed = $speed1+10;
? ? ? ? return $speed;
? ? }
}
echo Car::speedUp();
2017-08-07
問題所在:public $speed = 0; 要修改為靜態(tài)成員或者實例化之后才可調用