認真學習php物件導向-3
前言
準備寫一個認真學習php物件導向的系列,使用php來做網頁,沒有深入瞭解php的話,可能三板斧就夠了,並不需要有多高深。如有錯誤,歡迎各位不吝賜教。進度安排的話,我學到哪裡,就更新到哪裡了。形式的話就採用一個需求小案例,然後實現,並附上自己的總結,文章源碼 所用到的環境
系統:ubuntu16.04
編輯器:phpstorm2017
php7 需求 :1)使用自訂模板建立入口檔案 解決 : ob_start以及get_object_vars和extract的使用
ob_start
此函數將開啟輸出緩衝。當輸出緩衝啟用後,指令碼將不會輸出內容(除http標題外),相反需要輸出的內容被儲存在內部緩衝區中
我們可以使用此函數載入自訂的模板檔案,但並沒有輸出到螢幕中,而是用變數擷取資料,並將資料寫入到建立的入口檔案當中
get_object_vars
返回由對象屬性群組成的關聯陣列
可以擷取該對象的屬性值,並建立使用者輸入的參數檔案目錄以及寫入使用者的資料
extract
從數組中將變數匯入到當前的符號表,可以將數組直接轉換成變數( key)=值( key)=值(value) 實現 : 建立自訂模板檔案(template檔案下建立index.tpl) index.tpl
<?php echo '<?php' ?> /** * project name <?php echo $prj_name?> * user <?php echo $author?> * date <?php echo date('Y-m-d')?> */
寫入模板檔案所需要的變數值
god_class.hp
static function start() { $config=loadconfig(); $ff=new god_frame($config->prj_name); $ff->prj_name=$config->prj_name; $ff->author=$config->author; $ff->run(); }
在建立檔案目錄類對輸入的參數處理以及產生模板對應自訂檔案
god_frame.hp
function run() { !file_exists($this->project_folder) && mkdir($this->project_folder); !file_exists($this->project_main) && file_put_contents($this->project_main,''); extract(get_object_vars($this)); ob_start(); include (dirname(__FILE__).'/template/index.tpl'); $cnt=ob_get_contents(); ob_end_clean(); file_put_contents($this->project_main,$cnt); }
效果 :
需求 :2)使用php內建伺服器部署網站,供開發測試使用 解決 : system和php命令列啟動內建伺服器命令
system
同 C 版本的 system() 函數一樣, 本函數執行 command 參數所指定的命令, 並且輸出執行結果。
啟動php內建伺服器
php啟動內建伺服器方法
php -S “可以隨意設定訪問的地址” -t 項目的目錄位址
如:php -S localhost:8080 -t /home/shisiying/code/phpOrinentedObject/app 實現 : 使用system函數進行執行 god_frame.hp
function run() { !file_exists($this->project_folder) && mkdir($this->project_folder); !file_exists($this->project_main) && file_put_contents($this->project_main,''); extract(get_object_vars($this)); ob_start(); include (dirname(__FILE__).'/template/index.tpl'); $cnt=ob_get_contents(); ob_end_clean(); file_put_contents($this->project_main,$cnt); echo "the server is running!"; system("php -S localhost:8080 -t /home/shisiying/code/phpOrinentedObject/app"); }
效果 :
其中it works在產生的後續添加的,方便觀察效果
需求 :3)對唯寫變數的檔案類比編譯 解決 : scandir和get_defined_vars()和var_export的使用
scandir
列出指定路徑中的檔案和目錄
get_defined_vars
返回由所有已定義變數所組成的數組
var_export
輸出或返回一個變數的字串表示,函數的第二個參數設定為 TRUE,從而返回變數的表示。 實現 : 建立一個檔案
** god_frame.php
function compile() { $_files=scandir($this->project_folder.'/code'); foreach ($_files as $_file) { if (preg_match("/\w+\.var\.php$/i",$_file)) { require ($this->project_folder.'/code/'.$_file); unset($_file); } } unset($_files); $result='<?php'.PHP_EOL .'extract('.var_export(get_defined_vars(),1).');'; file_put_contents($this->project_folder.'/vars',$result); }
god_class.php
//類比編譯 static function compile() { $config=loadconfig(); $gf=new god_frame($config->prj_name); $gf->compile(); }
效果 :
需求 :4)對唯寫函數的檔案類比編譯 解決 : scandir和get_defined_function()和ReflectionFunction的使用
scandir
列出指定路徑中的檔案和目錄
get_defined_vars
返回由所有已定義變數所組成的數組
var_export
輸出或返回一個變數的字串表示,函數的第二個參數設定為 TRUE,從而返回變數的表示。
get_defined_function()
返回所有已定義函數的數組
ReflectionFunction
報告了一個函數的有關資訊。使用方法如下
array_slice
從數組中取出一段,數組的截取函數 implode
將一個一維數組的值轉化為字串 unset
釋放給定的變數 實現 : 建立一個函數檔案
god_frame.php
function compile() { $_files=scandir($this->project_folder.'/code'); foreach ($_files as $_file) { if (preg_match("/\w+\.(var|func)\.php$/i",$_file)) { require ($this->project_folder.'/code/'.$_file); unset($_file); } } unset($_files); $result='<?php'.PHP_EOL .'extract('.var_export(get_defined_vars(),1).');'; file_put_contents($this->project_folder.'/vars',$result); $funs=get_defined_functions(); foreach ($funs['user'] as $fun) { $f=new \ReflectionFunction($fun); $start=$f->getStartLine(); $end=$f->getEndLine(); $fileList=file($f->getFileName()); $re = '<?php'.PHP_EOL .implode(array_slice($fileList,$start-1,$end-$start+1)); file_put_contents($this->project_folder.'/func',$re); } }
效果 :
需求 :5)類比簡單的mvc模式,接收瀏覽器訪問的mv並進行路由簡單處理 解決 : $_SERVER[‘PATH_INFO’]和explode的使用 實現 : 建立一個class檔案
index.php
$pi=$_SERVER['PATH_INFO']; $controller=explode('/',$pi)[1]; $method=explode('/',$pi)[2]; require (getcwd().'/code/'.$controller.".class.php"); $get_class=new $controller(); $get_class->$method();
效果 :