認真學習php物件導向-2
前言
準備寫一個認真學習php物件導向的系列,使用php來做網頁,沒有深入瞭解php的話,可能三板斧就夠了,並不需要有多高深。如有錯誤,歡迎各位不吝賜教。進度安排的話,我學到哪裡,就更新到哪裡了。形式的話就採用一個需求小案例,然後實現,並附上自己的總結,文章源碼 所用到的環境
系統:ubuntu16.04
編輯器:phpstorm2017 需求 :1)參數容錯以及自動化調用方法 解決 : __callstatic的使用以及構造調用函數 __callstatic在調用一個類不存在的靜態函數的時候會觸發這個函數的執行,我們可以使用這個對輸入一個不存在的參數的時候進行友好報錯處理。 自動化調用,參數不多的時候,我們可以在類中指定函數名,但是當函數過多的時候,這種調用明顯很費力,我們可以採取建構函式名的方法,對函數進行調用 實現 : 自動化調用 god.hp
require ("god_calss.php"); $result=''; if ($argc>=2) { $p=$argv[1]; if(substr($p,0,1)=='-') { $p=substr($argv[1],1); $result=god_calss::$p(); } else { $result="you shuold add - to call the function!"; } // '-v'==$argv[1] && $result =god_calss::version(); // '-init'==$argv[1] && god_calss::init(); // '-make'==$argv[1] && god_calss::make(); } echo $result; echo PHP_EOL;
容錯處理
god_class.hp
static function __callStatic($name, $arguments) { // TODO: Implement __callStatic() method. echo "the function you call is undefined!"; }
效果 :
需求 :2)使用stdclass臨時儲存改造init 解決 : stdClass stdClass 在PHP核心進行模組初始化操作時會自動載入這個函數,它的所有的魔術方法,父類、介面等在初始化時都被設定成NULL。由於在PHP中對於一個類我們無法動態添加方法, 所以這個類只能用來處理動態屬性 實現 : 改造init god_class.hp
static function init() { echo "input your project_name?".PHP_EOL; $prj_name=fgets(STDIN); echo "input your author_name?".PHP_EOL; $author=fgets(STDIN); echo "your input:".PHP_EOL; echo json_encode(TC(array('prj_name'=>$prj_name,'author'=>$author))); }
####god_func.hp
//返回一個儲存使用者輸入的對象
function TC($p) { $get_class=new stdClass(); foreach ($p as $k=>$v) { $get_class->$k=$v; } return $get_class;}
god.hp
一定要再class檔案前面
require (“god_func.php”);
require (“god_calss.php”); 效果 :
需求 :3)接受使用者輸入的參數建立檔案夾 解決 : mkdir和file_get_contents和json_decode
具體用法可以查下官網手冊
1. 前面我們接受使用者輸入,並將其存入json檔案當中,現在要做的是從json檔案讀取資料,返回對象,建立使用者輸入對應的檔案夾 實現 : 改造init god_class.hp
static function start() { $config=loadconfig(); //建立檔案夾 !file_exists(getcwd().'/'.$config->prj_name) && mkdir(getcwd().'/'.$config->prj_name); //建立檔案 !file_exists(getcwd().'/'.$config->prj_name.'/index.php') && file_put_contents(getcwd().'/'.$config->prj_name.'/index.php',''); }
####god_func.hp
function loadconfig(){ $file=file_get_contents(getcwd().'/god.json'); return json_decode($file); }
效果 :
需求 :4)自動引入使用者需要調用的類檔案 解決 : __autoload和命名空間namespace的使用
將上述建立檔案自訂了一個類,為了更有規劃的搭建整個項目的架構,專案檔目錄變成這樣子,core代表項目的核心檔案目錄,frame就是我們所引用的外部類庫檔案目錄,template就是模板檔案目錄
實現 : 改造init god_class.hp
在類名外面寫入如下代碼
use core\frame\god_frame; function __autoload($className) { $className=str_replace('\\','/',$className).'.php'; require($className); }
方法調用:
static function start() { $config=loadconfig(); $ff=new god_frame($config->prj_name); $ff->run(); }
####god_frame.hp
namespace core\frame;class god_frame{ public $project_folder="";//項目入口檔案 public $project_main="";//入口檔案 function __construct($prjName) { $this->project_folder=getcwd()."/".$prjName; $this->project_main=$this->project_folder.'/index.php'; } function run() { !file_exists($this->project_folder) && mkdir($this->project_folder); !file_exists($this->project_main) && file_put_contents($this->project_main,''); }}
效果 :