谁能告诉我为什么Codeigniter的Model类几乎是空的?该设计完成什么工作?
1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2: /**
3: * CodeIgniter
4: *
5: * An open source application development framework for PHP 5.1.6 or newer
6: *
7: * @package CodeIgniter
8: * @author ExpressionEngine Dev Team
9: * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10: * @license http://codeigniter.com/user_guide/license.html
11: * @link http://codeigniter.com
12: * @since Version 1.0
13: * @filesource
14: */
15:
16: // ------------------------------------------------------------------------
17:
18: /**
19: * CodeIgniter Model Class
20: *
21: * @package CodeIgniter
22: * @subpackage Libraries
23: * @category Libraries
24: * @author ExpressionEngine Dev Team
25: * @link http://codeigniter.com/user_guide/libraries/config.html
26: */
27: class CI_Model {
28:
29: /**
30: * Constructor
31: *
32: * @access public
33: */
34: function __construct()
35: {
36: log_message('debug', "Model Class Initialized");
37: }
38:
39: /**
40: * __get
41: *
42: * Allows models to access CI's loaded classes using the same
43: * syntax as controllers.
44: *
45: * @param string
46: * @access private
47: */
48: function __get($key)
49: {
50: $CI =& get_instance();
51: return $CI->$key;
52: }
53: }
54: // END Model Class
55:
56: /* End of file Model.php */
57: /* Location: ./system/core/Model.php */
我看到了__get函数,但是我不确定这样做对我有帮助。扩展此类将对我的设计有何帮助?
39: /**
40: * __get
41: *
42: * Allows models to access CI's loaded classes using the same
43: * syntax as controllers.
44: *
45: * @param string
46: * @access private
47: */
48: function __get($key)
49: {
50: $CI =& get_instance();
51: return $CI->$key;
52: }
因此,每当扩展CI_Model
类时,您的模型都将继承__construct
和__get
函数,两者都是Magic Methods。
该__construct
每当你在模型中调用一个函数功能将被调用。它所做的只是创建一条日志消息。
function __construct()
{
log_message('debug', "Model Class Initialized");
}
但是它很有用,因为说您在控制器中调用模型,由于某种原因它无法正常工作。
$this->load->model('Model_name');
$this->Model_name->function();
至少您将能够检查日志以查看是否已加载模型-对调试很有用。
该__get
方法用于
用于从无法访问的属性读取数据。
同样,这很有用,因为它将允许您访问模型中的任何CI库。例如,您将能够在模型-中使用会话库$this->session->userdata('username')
。如果没有该__get
函数CI_Model
尝试访问模型中的会话库,则会出错。但是,通过扩展CI_Model
它不会。所以记住这是非常有用的
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句