亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php構(gòu)造函數(shù)實(shí)例講解

 更新時(shí)間:2013年11月13日 11:10:51   作者:  
本文將使用實(shí)例講解php構(gòu)造函數(shù)的使用方法

PHP官網(wǎng)定義:

復(fù)制代碼 代碼如下:

構(gòu)造函數(shù)是類中的一個(gè)特殊函數(shù),當(dāng)使用 new 操作符創(chuàng)建一個(gè)類的實(shí)例時(shí),構(gòu)造函數(shù)將會(huì)自動(dòng)調(diào)用。當(dāng)函數(shù)與類同名時(shí),這個(gè)函數(shù)將成為構(gòu)造函數(shù)。如果一個(gè)類沒有構(gòu)造函數(shù),則調(diào)用基類的構(gòu)造函數(shù),如果有的話,則調(diào)用自己的構(gòu)造函數(shù)

如a.php一個(gè)class a類:
復(fù)制代碼 代碼如下:

<?php
class a{
 function __construct(){
  echo 'class a';
 }
}

b.php有個(gè)class b類繼承a類:
復(fù)制代碼 代碼如下:

<?php
include 'a.php';
class b extends a{
 function __construct(){
  echo '666666';
  //parent::__construct();
 }

 function index(){
  echo 'index';
 }
}
 

$test=new b();
這樣寫的話,b類有自己的構(gòu)造函數(shù),那么實(shí)例化b類的時(shí)候,自動(dòng)運(yùn)行構(gòu)造函數(shù),此時(shí)默認(rèn)不運(yùn)行父類的構(gòu)造函數(shù),如果同時(shí)要運(yùn)行父類構(gòu)造函數(shù),要聲明parent::__construct();
復(fù)制代碼 代碼如下:

<?php
include 'a.php';
class b extends a{
 function index(){
  echo 'index';
 }
}
 

$test=new b();
此時(shí)b類沒有自己的構(gòu)造函數(shù),那么將默認(rèn)執(zhí)行父類的構(gòu)造函數(shù)。

相關(guān)文章

最新評(píng)論