php構(gòu)造函數(shù)實例講解
更新時間:2013年11月13日 11:10:51 作者:
本文將使用實例講解php構(gòu)造函數(shù)的使用方法
PHP官網(wǎng)定義:
復制代碼 代碼如下:
構(gòu)造函數(shù)是類中的一個特殊函數(shù),當使用 new 操作符創(chuàng)建一個類的實例時,構(gòu)造函數(shù)將會自動調(diào)用。當函數(shù)與類同名時,這個函數(shù)將成為構(gòu)造函數(shù)。如果一個類沒有構(gòu)造函數(shù),則調(diào)用基類的構(gòu)造函數(shù),如果有的話,則調(diào)用自己的構(gòu)造函數(shù)
如a.php一個class a類:
復制代碼 代碼如下:
<?php
class a{
function __construct(){
echo 'class a';
}
}
b.php有個class b類繼承a類:
復制代碼 代碼如下:
<?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ù),那么實例化b類的時候,自動運行構(gòu)造函數(shù),此時默認不運行父類的構(gòu)造函數(shù),如果同時要運行父類構(gòu)造函數(shù),要聲明parent::__construct();
復制代碼 代碼如下:
<?php
include 'a.php';
class b extends a{
function index(){
echo 'index';
}
}
$test=new b();
此時b類沒有自己的構(gòu)造函數(shù),那么將默認執(zhí)行父類的構(gòu)造函數(shù)。
相關(guān)文章
PHP XML error parsing SOAP payload on line 1
PHP中GBK頁面調(diào)用WebService的編碼問題:XML error parsing SOAP payload on line 12010-06-06
php實現(xiàn)二進制和文本相互轉(zhuǎn)換的方法
這篇文章主要介紹了php實現(xiàn)二進制和文本相互轉(zhuǎn)換的方法,實例分析了文本與數(shù)制轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
利用discuz自帶通行證整合dedecms的方法以及文件下載
利用discuz自帶通行證整合dedecms的方法以及文件下載...2007-03-03

