color = $color; } 函數what_color() { return $this->color; }}$汽車=新的"/> color = $color; } 函數what_color() { return $this->color; }}$汽車=新的">

首頁  >  文章  >  後端開發  >  關於php版本號碼的錯誤詳解

關於php版本號碼的錯誤詳解

零下一度
零下一度原創
2017-07-21 13:21:231506瀏覽
<?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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn