php的基礎問題請教一下

來源:互聯網
上載者:User
class Test1 {static private $_name;}$_test1 = new Test1('張三');var_dump($_test1);


頁面輸出的是:
object(Test1)#1 (0) { }
我執行個體化的時候還傳遞了參數,但是Test1的類中我沒有寫構造方法__construct(),為什麼不會報錯呢?



回複討論(解決方案)

構造方法也不是必須的,你傳參數進去,它不執行就是了

建構函式已知都在,你不定義只是那個函數為空白。

構造方法也不是必須的,你傳參數進去,它不執行就是了



基礎不怎麼牢,麻煩請舉個簡單的例子可以嗎,謝謝!

class Test1 {    static private $_name;    public $_date;    public function __construct() {    $this->_date = 123;    } }$_test1 = new Test1('張三');var_dump($_test1);

我記得執行個體化類的時候,建構函式不是就執行了嗎
為什麼參數不對應啊?


class Test1 {
static private $_name;
}
並沒有定義建構函式 __construct

class Test1 {    private static $_name;    public $_date;    public function __construct($name) {        $this->_date = 123;self::$_name = $name;    }function output(){return self::$_name.' - '.$this->_date;}} $_test1 = new Test1('張三');echo $_test1->output();
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.