简体   繁体   中英

Cannot use __get / __set function in CodeIgniter

I have a class inherited from CI_Model as follows:

<?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;
    }
}

When I run my code, I have an error message:

Type: Error

Message: Call to a member function database() on null

Filename: C:\\xampp\\htdocs\\hostelry_pms\\application\\models\\DBObject.php

Line Number: 11

But when I comment the __get / __set functions, the code can run without problem.

Any idea?

CI_Model has its own __get function already defined .

By overriding it (and totally changing how it works), chances are you've broken everything in CI that expects that magic method to work a certain way. $this->load is probably one of those now-broken things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM