《PHP編程:PHP新建類問(wèn)題分析及解決思路》要點(diǎn):
本文介紹了PHP編程:PHP新建類問(wèn)題分析及解決思路,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP應(yīng)用下面先給大家分析php新建類的問(wèn)題
PHP應(yīng)用index.php文件
PHP應(yīng)用 function __autoload($_className) { require $_className.'.class.php'; } //新建類?? if (isset($_GET['index'])) { $m=new Main($_GET['index']); }else{ $m=new Main(); } include $m->ui();
PHP應(yīng)用main.class.php文件
PHP應(yīng)用
class Main{
private $index;
//構(gòu)造辦法,初始化數(shù)據(jù)
public function __construct($index=''){
$this->index=$index;
}
//ui函數(shù)include相應(yīng)的包含文件
public function ui(){
if(empty($this->index)||!file_exists($this->index.'.inc')){
$this->index='start';
}
return $this->index.'.inc';
}
}
PHP應(yīng)用紅字的部分有啥意義了:類中構(gòu)造函數(shù)傳參值已設(shè)默認(rèn)是空(public function __construct($index='')),為啥不能直接寫$m=new Main($_GET['index']);.如果不想在index做紅字的if判斷,類里需要怎么寫了.謝謝,不是太理解
PHP應(yīng)用------辦理思路----------------------
PHP應(yīng)用
if (isset($_GET['index'])) {
$m=new Main($_GET['index']); //如果 $_GET['index'] 存在則將 $_GET['index'] 作為參數(shù)
}else{
$m=new Main(); //不然使用默認(rèn)參數(shù)
}
PHP應(yīng)用直接使用 $_GET['index'] 將可能引發(fā) NOTICE 級(jí)別錯(cuò)誤
PHP應(yīng)用不加區(qū)別的使用傳入數(shù)據(jù),可能引發(fā)平安問(wèn)題
PHP應(yīng)用------辦理思路----------------------
PHP應(yīng)用稍微改了一下你看咋樣.
PHP應(yīng)用
<?php
class Main{
private $index;
//構(gòu)造辦法,初始化數(shù)據(jù)
public function __construct($index='')
{
$this->index=$index?$index:'';
}
//ui函數(shù)include相應(yīng)的包含文件
public function ui()
{
if(empty($this->index)
PHP應(yīng)用------解決思路----------------------
PHP應(yīng)用
!file_exists($this->index.'.inc'))
{
$this->index='start';
}
return $this->index.'.inc';
}
}
PHP應(yīng)用ps:php怎么創(chuàng)建文件?
PHP應(yīng)用php項(xiàng)目開(kāi)發(fā)過(guò)程中,常常必要自動(dòng)創(chuàng)建一些文件,如生成靜態(tài)html,生成php緩存文件,生成txt文件等等.下面就分享一下如何利用php程序創(chuàng)建文件,并向文件中寫入內(nèi)容.
PHP應(yīng)用一個(gè)項(xiàng)目中,可能不止一次必要生成文件,因此我們可以定義一個(gè)函數(shù),當(dāng)必要?jiǎng)?chuàng)建文件時(shí)再來(lái)調(diào)用這個(gè)函數(shù),即可.
PHP應(yīng)用步調(diào)一、定義函數(shù)writefile,用于以寫的方式打開(kāi)一個(gè)文件,文件不存在時(shí)自動(dòng)創(chuàng)建,并向文件寫入內(nèi)容,代碼如下.
PHP應(yīng)用
<?php
function writefile($fname,$str){
$fp=fopen($fname,"w");
fputs($fp,$str);
fclose($fp);
}
?>
PHP應(yīng)用步調(diào)二、函數(shù)的使用.如創(chuàng)建test.txt文件,并寫入內(nèi)容“abc”,代碼如下:
PHP應(yīng)用
<?php
$filename='test.txt';
$str='abc';
writefile($filename,$str);
?>
PHP應(yīng)用通過(guò)上述兩個(gè)步調(diào)的操作,即可實(shí)現(xiàn)php創(chuàng)建文件的功能.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP編程:PHP新建類問(wèn)題分析及解決思路》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/8371.html