$this和self的区别

php中this不能用于static的方法中,而self只能用于静态方法中:

class Car{
private static $speed = 10;
var $name = "haha";
public function getSpeed(){
echo $this->speed;  //失败
}
static function speedUp(){
echo self::$speed;
echo "
";

}
function show(){
echo $this->name; 
}
}


Car::speedUp();  //10
$car = new Car();
$car->show();  //haha
$car->getSpeed(); //无显示

你可能感兴趣的:($this和self的区别)