簡體   English   中英

無法在CodeIgniter中使用__get / __set函數

[英]Cannot use __get / __set function in CodeIgniter

我有一個從CI_Model繼承的類,如下所示:

<?php
class DBObject extends CI_Model {
    // fields
    public $errorMsg;
    protected $_columnsConfig;
    protected $_tableName;
    // functions
    // constructor
    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    // getter
    public function __get($name) { 
        if (strtolower($name) == "columncount") { 
            return count($this->_columnsConfig);
        } else { 
            $colCfg = null;
            $i = 0;
            while ($i < count($this->_columnsConfig)) { 
                $dbcc = $this->_columnsConfig[$i];
                if (strtolower($name) == strtolower($dbcc->propertyName)) { 
                    $colCfg = $dbcc;
                    break;
                }
                $i++;
            }
            if ($colCfg != null) return $colCfg->storedValue;
            else return null;
        }
    }
    // setter
    function __set($name, $value) { 
        $colCfg = null;
        $i = 0;
        while ($i < count($this->_columnsConfig)) { 
            $dbcc = $this->_columnsConfig[$i];
            if (strtolower($name) == strtolower($dbcc->propertyName)) { 
                $colCfg = $dbcc;
                break;
            }
            $i++;
        }
        if ($colCfg != null) $colCfg->newValue = $value;
    }
}

運行代碼時,出現錯誤消息:

類型:錯誤

消息:在null上調用成員函數database()

文件名:C:\\ xampp \\ htdocs \\ hostelry_pms \\ application \\ models \\ DBObject.php

行號:11

但是,當我注釋__get / __set函數時,代碼可以正常運行。

任何想法?

CI_Model 已經定義了自己的__get函數。

通過覆蓋它(並完全改變它的工作方式),您很有可能破壞了CI中所有希望該魔術方法以某種方式起作用的東西。 $this->load可能是這些現在已經中斷的事情之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM