久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php構(gòu)造函數(shù)的寫法是什么

      在php中,構(gòu)造函數(shù)是類中的一種特殊函數(shù),具體寫法為“public function __construct(參數(shù)列表){…代碼…}”;參數(shù)列表是可選的,不需要時(shí)可以省略。

      php構(gòu)造函數(shù)的寫法是什么

      本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

      構(gòu)造函數(shù)(constructor method,也稱為構(gòu)造器)是類中的一種特殊函數(shù),當(dāng)使用 new 關(guān)鍵字實(shí)例化一個(gè)對(duì)象時(shí),構(gòu)造函數(shù)將會(huì)自動(dòng)調(diào)用。

      在 PHP3.0 和 PHP4.0 中,構(gòu)造函數(shù)是一個(gè)與其所在類同名的函數(shù)。而在 PHP5 中,雖然也支持 PHP3.0 和 PHP4.0 中的用法,但是更推薦使用__construct作為類的構(gòu)造函數(shù),這樣做的好處就是構(gòu)造函數(shù)無需隨著類名的改變而做出修改。在 PHP7.0 中廢棄了 PHP3.0 和 PHP4.0 中的用法,構(gòu)造函數(shù)必須使用__construct來定義。

      構(gòu)造函數(shù)就是當(dāng)對(duì)象被創(chuàng)建時(shí),類中被自動(dòng)調(diào)用的第一個(gè)函數(shù),并且一個(gè)類中只能存在一個(gè)構(gòu)造函數(shù)。和普通函數(shù)類似構(gòu)造函數(shù)也可以帶有參數(shù),如果構(gòu)造函數(shù)有參數(shù)的話,那么在實(shí)例化也需要傳入對(duì)應(yīng)的參數(shù),例如new Students($name, $age)。

      創(chuàng)建構(gòu)造函數(shù)的語法格式如下:

      public function __construct(參數(shù)列表){     ... ... }

      其中,參數(shù)列表是可選的,不需要時(shí)可以省略。

      如果沒有在代碼中顯式地聲明構(gòu)造函數(shù),類中會(huì)默認(rèn)存在一個(gè)沒有參數(shù)列表并且內(nèi)容為空的構(gòu)造函數(shù)。如果顯式地聲明構(gòu)造函數(shù)則類中的默認(rèn)構(gòu)造方法將不會(huì)存在。所以構(gòu)造函數(shù)通常用來做一些準(zhǔn)備工作,比如為某些參數(shù)賦值等。

      注意:如果顯式地聲明構(gòu)造函數(shù),那么它的訪問權(quán)限必須是 public,而且構(gòu)造函數(shù)是在實(shí)例化時(shí)自動(dòng)調(diào)用的,我們不需要手動(dòng)調(diào)用。

      【示例】創(chuàng)建一個(gè)類,并為其顯示的創(chuàng)建構(gòu)造函數(shù),代碼如下:

      <?php     class Website{         public $name, $url, $title;         public function __construct($str1, $str2, $str3){             $this -> name  = $str1;             $this -> url   = $str2;             $this -> title = $str3;             $this -> demo();         }         public function demo(){             echo $this -> name.'<br>';             echo $this -> url.'<br>';             echo $this -> title.'<br>';         }     }     $object = new Website('PHP中文網(wǎng)','https://www.php.cn/','構(gòu)造函數(shù)'); ?>

      運(yùn)行結(jié)果如下:

      PHP中文網(wǎng) https://www.php.cn/ 構(gòu)造函數(shù)

      代碼中我們用到了 $this,它表示當(dāng)前調(diào)用的對(duì)象,而且 $this 只能在類的方法中使用

      推薦學(xué)習(xí):《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)