|
| 1 | +<!DOCTYPE html> |
| 2 | +<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> |
| 3 | +<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> |
| 4 | +<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> |
| 5 | +<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> |
| 6 | + <head> |
| 7 | + <meta charset="utf-8"> |
| 8 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 9 | + <title></title> |
| 10 | + <meta name="description" content=""> |
| 11 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 12 | + |
| 13 | + <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> |
| 14 | + |
| 15 | + <link rel="stylesheet" href="css/normalize.css"> |
| 16 | + <link rel="stylesheet" href="css/main.css"> |
| 17 | + <script src="js/vendor/modernizr-2.6.2.min.js"></script> |
| 18 | + |
| 19 | + <link rel="stylesheet" href="css/bootstrap.css"> |
| 20 | + <link rel="stylesheet" href="css/shCore.css"> |
| 21 | + <link rel="stylesheet" href="css/shThemeDefault.css"> |
| 22 | + <script src="js/shCore.js"></script> |
| 23 | + <script src="js/shBrushPhp.js"></script> |
| 24 | + |
| 25 | + </head> |
| 26 | + <body> |
| 27 | + <!--[if lt IE 7]> |
| 28 | + <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> |
| 29 | + <![endif]--> |
| 30 | + |
| 31 | + <!-- Add your site or application content here --> |
| 32 | + <div class='alert alert-success'> |
| 33 | + 我們開始分析core/Controller.php。<br/> |
| 34 | + 在開發application時,在model、controller等地方會寫到的$this就是繼承這個本體物件。<br/> |
| 35 | + 甚至在撰寫library時,常常為了使用ci本身的helper、model等功能,寫出 $this->ci =& get_instance() 這樣的程式碼。<br/> |
| 36 | + |
| 37 | + </div> |
| 38 | + |
| 39 | + <!-- You also need to add some content to highlight, but that is covered elsewhere. --> |
| 40 | + <pre class="brush: php"> |
| 41 | +<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 42 | +/** |
| 43 | + * CodeIgniter |
| 44 | + * |
| 45 | + * An open source application development framework for PHP 5.1.6 or newer |
| 46 | + * |
| 47 | + * @package CodeIgniter |
| 48 | + * @author ExpressionEngine Dev Team |
| 49 | + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
| 50 | + * @license http://codeigniter.com/user_guide/license.html |
| 51 | + * @link http://codeigniter.com |
| 52 | + * @since Version 1.0 |
| 53 | + * @filesource |
| 54 | + */ |
| 55 | + |
| 56 | +// ------------------------------------------------------------------------ |
| 57 | + |
| 58 | +/** |
| 59 | + * CodeIgniter Application Controller Class |
| 60 | + * |
| 61 | + * This class object is the super class that every library in |
| 62 | + * CodeIgniter will be assigned to. |
| 63 | + * |
| 64 | + * @package CodeIgniter |
| 65 | + * @subpackage Libraries |
| 66 | + * @category Libraries |
| 67 | + * @author ExpressionEngine Dev Team |
| 68 | + * @link http://codeigniter.com/user_guide/general/controllers.html |
| 69 | + */ |
| 70 | +class CI_Controller { |
| 71 | + |
| 72 | + private static $instance; |
| 73 | + |
| 74 | + /** |
| 75 | + * Constructor |
| 76 | + */ |
| 77 | + public function __construct() |
| 78 | + { |
| 79 | + self::$instance =& $this; |
| 80 | + |
| 81 | + // Assign all the class objects that were instantiated by the |
| 82 | + // bootstrap file (CodeIgniter.php) to local class variables |
| 83 | + // so that CI can run as one big super object. |
| 84 | + foreach (is_loaded() as $var => $class) |
| 85 | + { |
| 86 | + $this->$var =& load_class($class); |
| 87 | + } |
| 88 | + </pre> |
| 89 | + <div class='alert alert-success'> |
| 90 | + 將之前載入的類別全部與這個物件接在一起。<br/> |
| 91 | + 開發者因而可以使用$this->router、$this->uri、$this->config等成員變數。<br/> |
| 92 | + 不過,開發者想要繼續用$RTR =& load_class('Router', 'core')以及$URI =& load_class('URI', 'core')這種寫法,也是可以啦。 |
| 93 | + </div> |
| 94 | + |
| 95 | + <!-- You also need to add some content to highlight, but that is covered elsewhere. --> |
| 96 | + <pre class="brush: php"> |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + $this->load =& load_class('Loader', 'core'); |
| 101 | + |
| 102 | + $this->load->initialize(); |
| 103 | + |
| 104 | + log_message('debug', "Controller Class Initialized"); |
| 105 | + } |
| 106 | + </pre> |
| 107 | + <div class='alert alert-success'> |
| 108 | + 最後載入Loader類別,開發者讀取model、library、helper等類別都靠它。<br/> |
| 109 | + 參見:<a href='loader.html'>統一的載入介面:core/Loader.php</a> |
| 110 | + </div> |
| 111 | + <div class='alert alert-danger'> |
| 112 | + 那一行$this->load->initialize()很莫名其妙。前面用load_class載入類別,並不會在之後呼叫任何初始化函數,而是直接寫在建構式內。<br/> |
| 113 | + 這邊卻是在Loader類別的建構式執行一些動作,之後呼叫initialize再執行一些動作。<br/> |
| 114 | + 也就是說「load_class」到底是純粹載入類別?還是載入的同時順便執行初始工作?<br/> |
| 115 | + 可說是多此一舉,並且意義不一致。<br/> |
| 116 | + </div> |
| 117 | + |
| 118 | + <!-- You also need to add some content to highlight, but that is covered elsewhere. --> |
| 119 | + <pre class="brush: php"> |
| 120 | + |
| 121 | + |
| 122 | + public static function &get_instance() |
| 123 | + { |
| 124 | + return self::$instance; |
| 125 | + } |
| 126 | +} |
| 127 | +// END Controller class |
| 128 | + |
| 129 | +/* End of file Controller.php */ |
| 130 | +/* Location: ./system/core/Controller.php */ |
| 131 | + |
| 132 | + |
| 133 | + </pre> |
| 134 | + |
| 135 | + <div class='alert alert-danger'> |
| 136 | + |
| 137 | + </div> |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + <!-- Finally, to actually run the highlighter, you need to include this JS on your page --> |
| 143 | + <script type="text/javascript"> |
| 144 | + SyntaxHighlighter.all() |
| 145 | + </script> |
| 146 | + |
| 147 | + </body> |
| 148 | + <style> |
| 149 | + body{ |
| 150 | + width: 960px; |
| 151 | + margin: 0 auto; |
| 152 | + background-color: #444; |
| 153 | + } |
| 154 | + .alert{ |
| 155 | + line-height: 2; |
| 156 | + } |
| 157 | + |
| 158 | + </style> |
| 159 | +</html> |
0 commit comments