<?php class Car { var $color = "add"; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; } } $car = new Car; echo $car->what_color(),"<br>over"; ?>
PHP版本號碼
#php 7.0.10
所報錯誤
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Car has a deprecated constructor in E:\phpStorm\firstPhp\test.php# on line 8
解決方式
#查閱資料,發現php7.0之後將不再支援與類別名稱相同的建構方法,建構方法統一使用__construct()。
#修正後程式碼
<?php class Car { public $color = "add"; function __construct($color="green") { //注意是双下划线$this->color = $color; } public function what_color() { return $this->color; } } $car = new Car("red"); echo $car->what_color(),"<br>over"; ?>
以上是關於php版本號碼的錯誤詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!