[首次分析]PHP写框架
生活随笔
收集整理的這篇文章主要介紹了
[首次分析]PHP写框架
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
index.php
<?php/* @Author HLZ* @Time 2016年8月3日 15:00:35* @Description 框架價格分析* 初始目錄 分三個部分 * ①index.php * 入口文件設置,且統一命名空間* a.設置常量* define("HLZ_PATH",realpath(__DIR__));//獲取入口目錄位置* define("Mysql帳號密碼");* b.引入自己寫的Model、View、Controller基類* ②App文件夾 分四個文件夾* a.供引入的類庫* b.Controller* c.View* d.Logic* 要求:其命名空間與類名恰好能找到文件對應.class.php文件* 統一自動引入機制,與框架搭建過程中常用的方法,詳見下文* */define("HLZ_PATH",realpath(__DIR__."\\.."));//這里只有一個文件,我就當作只有Controller目錄吧//Start.注意:這個是文件加載的方法,只能放在類外面 function __autoload($className){ //使用前提,命名空間得是 根,即namespace \;//Start:當實例化,對應的類,不存在于該php文件時,自動調用$file_where = HLZ_PATH."/".$className.'.class.php';$file_where=str_replace('\\',"/",$file_where);//轉義成可以require的格式if(file_exists($file_where)){require_once "$file_where";}echo "已經require $file_where"; }//a.接口類 interface test{ //1.1.定義接口,interface//接口算是一個抽象類,但是接口中每個被抽象的方法,都必須被繼承的類,全部實現function verify($username);//驗證用戶名function info();//驗證信息 } //b.接口邏輯類 class hlz_vip implements test{ //1.2.對應接口,實現,implementsprivate $string="VIP Interface Test!";public function verify($username){if($username==""){return '您未填寫用戶名';}return "Hello ".$username."!";}public function info(){return $this->string; } } //c.普通類 class show_info{private $username;public function show($username){$this->username= $username;$user_interface= new hlz_vip(); //2.1.1接口的調用$msg['Status'] = $user_interface->verify($username);$msg['Info'] = $user_interface->info();echo json_encode($msg);return $this; //3.0.鏈式調用,連貫操作,常應用于數據庫操作}public function say(){echo "<h3>鏈式調用 成功!</h3>";}public function __construct(){//3.1.當實例化對象時,自動調用,【可以用與類名一樣的函數來賴實現】echo "<h3>對象創建 成功!</h3><br/>";}public function __call($function_name, $args){ //4.1.當調用方法不存在時,自動調用echo "<h5>你所調用的函數:$function_name ,在該對象中不存在!</h5>";echo "<small>你剛剛輸入的參數為:<br/>";print_r($args);echo "</small>";}public function __set($property_name, $value) {//5.1.當變量不存在與對象中 或者 變量為該對象的私有屬性的時,自動調用return $this->$property_name = $value;}public function __get($property_name) {//6.1.當變量不存在與對象中 或者 變量為該對象的私有屬性的時,自動調用return isset($this->$property_name) ? $this->$property_name : null;}public function __isset($property_name){//7.1.在類外部使用 isset() 函數來測定對象里面的私有成員是否被設定時,自動調用return isset($this->$property_name);} } $test = new show_info(); $test->show("HLZ")->say();//測試1:鏈式調用 $test->hlz("參數一","參數二");//測試2:這個函數不存在于對象中 $test= new \API\HLZ(); //測試3:未初始化引入對應類文件HLZ.class.php
<?php namespace API; echo "自己加載成功"; class HLZ{function __construct(){echo "test類調用成功!";} }自動引入文件的問題
之前在調用的頁面,沒給命名空間,我們是用到的
function __autoload($className){require "$className"; }辦到的
可是,平時我們寫框架的時候,加上命名空間是必須的
所以這里有一個新的方法由此而生
可以使用函數 spl_autoload_register 來注冊一個類中的方法來代替 __autoload具體如下
class require_file{static function load( $className ) {//一定得是靜態函數$file_where = HLZ_PATH."/".$className.'.class.php';$file_where=str_replace('\\',"/",$file_where);//轉義成可以require的格式if(file_exists($file_where)){require_once "$file_where";}echo "已經require $file_where";} } //登記自動加載的函數 //實例化對象時,如果沒有對應的類//命名空間的名稱、自動加載的類名、類名對應的靜態方法 spl_autoload_register( array(__NAMESPACE__."\\require_file","load") );總結
以上是生活随笔為你收集整理的[首次分析]PHP写框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis与Memcache的对比
- 下一篇: 说说JSON和JSONP,也许你会豁然开