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è)類沒(méi)有構(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類沒(méi)有自己的構(gòu)造函數(shù),那么將默認(rèn)執(zhí)行父類的構(gòu)造函數(shù)。
您可能感興趣的文章:
- PHP中構(gòu)造函數(shù)和析構(gòu)函數(shù)解析
- php基礎(chǔ)知識(shí):類與對(duì)象(3) 構(gòu)造函數(shù)和析構(gòu)函數(shù)
- php構(gòu)造函數(shù)與析構(gòu)函數(shù)
- PHP構(gòu)造函數(shù)與析構(gòu)函數(shù)用法示例
- php構(gòu)造函數(shù)的繼承方法
- Php 構(gòu)造函數(shù)construct的前下劃線是雙的_
- PHP高級(jí)對(duì)象構(gòu)建 多個(gè)構(gòu)造函數(shù)的使用
- PHP 構(gòu)造函數(shù)和析構(gòu)函數(shù)原理與用法分析
相關(guān)文章
PHP XML error parsing SOAP payload on line 1
PHP中GBK頁(yè)面調(diào)用WebService的編碼問(wèn)題:XML error parsing SOAP payload on line 12010-06-06
php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法
這篇文章主要介紹了php實(shí)現(xiàn)二進(jìn)制和文本相互轉(zhuǎn)換的方法,實(shí)例分析了文本與數(shù)制轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
利用discuz自帶通行證整合dedecms的方法以及文件下載
利用discuz自帶通行證整合dedecms的方法以及文件下載...2007-03-03

