<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 基類實戰--析構函數的使用 什么是析構函數?估計會有很多人還沒有用過 其實析構函數就是一個收尾的一個方法,我們實際用一個例子學一下.我們創建一個以下虛擬基類 ~~~ <?php namespace app\base\controller; use think\Cache; use think\Controller; use think\Session; use think\Loader; /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/4/12 * Time: 13:28 */ abstract class Base extends Controller { protected $error; //出錯時候的記錄 protected $log=[]; //要保存的記錄 protected $saveLog = false ; /** * Power by Mikkle * QQ:776329498 * @param string $code * @param array $data * @param string $msg * @return array */ static public function showReturnCode($code = '', $data = [], $msg = '') { $return_data = [ 'code' => '500', 'msg' => '未定義消息', 'data' => $code == 1001 ? $data : [], ]; if (empty($code)) return $return_data; $return_data['code'] = $code; if(!empty($msg)){ $return_data['msg'] = $msg; }else if (isset(ReturnCode::$return_code[$code]) ) { $return_data['msg'] = ReturnCode::$return_code[$code]; } return $return_data; } protected function addLog($code='',$msg=''){ $this->log[] =[ 'uuid' => $this->uuid, 'url' => $this->request->url(true), 'method' => $this->request->method(), 'data' => $this->getData(), 'ip' => $this->request->ip(), 'code'=>$code, 'desc' => $msg, ]; } protected function toSaveLog(){ $this->saveLog = true ; $this->addLog(); } protected function showReturnCodeWithSaveLog($code = '', $data = [], $msg = ''){ $this->saveLog = true ; $this->addLog($code,$msg); return self::showReturnCode($code, $data, $msg); } protected function getData(){ if ($this->request->isPost()){ return $this->request->post(); }else{ return $this->request->get(); } } protected function saveLogAction(){ if (!empty($this->log)){ foreach($this->log as $value){ dump($value); } } } public function __destruct() { // TODO: Implement __destruct() method. //記錄日志 if (!empty($this->log) && $this->saveLog == true){ $this->saveLogAction(); } } } ~~~ 然后我們建立一個新的控制器繼承這個基類,好,我們這個文件就一行代碼 ~~~ <?php /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/5/22 * Time: 14:30 */ namespace app\member\controller; use app\base\model\Member; use think\Cache; use think\Db; class Test extends Base { public function index(){ return $this->showReturnCodeWithSaveLog(1001,'演示析構函數成功了'); } } ~~~ 好現在我們運行一下這個新建的Text控制器的index方法 運行結果如下 ![](https://box.kancloud.cn/a348d274b9201743525dbd79992ba27b_499x218.png) > 不知道大家能看懂多少 先慢慢看吧. > 換句現在流行的一句話,碼農現在也是拼爹時代,老子搞好了,兒子就容易了 那現在我來解析一下整個運行過程 實現 我們運行 一個返回方法 ~~~ $this->showReturnCodeWithSaveLog(1001,'演示析構函數成功了'); ~~~ 這個返回方法更改了類的保存狀態,并調用的添加日志的方法 ~~~ protected function showReturnCodeWithSaveLog($code = '', $data = [], $msg = ''){ $this->saveLog = true ; $this->addLog($code,$msg); return self::showReturnCode($code, $data, $msg); } ~~~ 接著,整個方法運行最后將運行析構函數 ~~~ public function __destruct() { // TODO: Implement __destruct() method. //記錄日志 if (!empty($this->log) && $this->saveLog == true){ $this->saveLogAction(); } } ~~~ 在析構函數中,我們判斷了類的屬性是否要保存log記錄,并運行自定義的保存方法. >[info] 我這里只是拿出一個實例來講解析構函數的使用方法,會用是一回事,用活又是另一回事,后期課程如果有時間,我再講講析構函數和Hook等類的結合應用,來打造出來的另一番海闊天空... >[danger] 寫在最后的話,我這個例子只是利用析構函數記錄復雜的操作日志,更多的是作為演示使用. 切記 不能用它代替系統日志,一旦中途報錯 ,析構函數的方法就無法運行了.切記切記...
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看