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