Welcome Guest, Not a member yet? Register   Sign In
Overloading MY_Controller
#1

[eluser]Unknown[/eluser]
Hi everyone, new here, used CodeIgniter for quite a while.

I'm starting a new project, ofcourse using the latest Reactor from BitBucket. I'm trying to automate process of importing the URL segments into properties of my controllers. I use magic methods in MY_Controller to make it happen. This, however, triggers the following error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Indirect modification of overloaded property Test::$benchmark has no effect

Filename: core/Controller.php

Line Number: 46

Fatal error: Cannot assign by reference to overloaded object in C:\xampp\htdocs\wzr_achterkant\system\core\Controller.php on line 46


Using the following code
Code:
class MY_Controller extends CI_Controller {
    
    private $get_params = array(); // get parameters container
    protected $parameters = array('format'); // array of valid get parameters
    
    function __construct() {
        parent::__construct();
        
        $this->_parseGetParams();
    }
    
    // magic method to set parameter
    function __set($name, $value) {
        if (in_array($name, $parameters)) { // if it's a valid parameter
            $this->get_params[$name] = $value;
        }
    }
    
    // magic method to get parameters
    function __get($name) {
        if (array_key_exists($name, $this->get_params)) {
            return $this->get_params[$name];
        }
    }
    
    function __unset($name) {
        if (in_array($name, $parameters)) { // als het een geldige parameter is
            unset($this->get_params[$name]);
        }
    }
    
    function __isset($name) {
        return isset($this->get_params[$name]);
    }
    
    // parsing of get parameters
    function _parseGetParams() {
        $parameters = $this->uri->ruri_to_assoc();
        foreach($this->parameters as $valid_param) {
            if (isset($parameters[$valid_param]) && strlen($parameters[$valid_param]) > 0) {
                $this->$valid_param = $parameters[$valid_param];    
            }
        }
    }
}

The error states it fails at line 46 from the Core Controller. These are lines 41-48:

Code:
// Assign all the class objects that were instantiated by the
        // bootstrap file (CodeIgniter.php) to local class variables
        // so that CI can run as one big super object.
        foreach (is_loaded() as $var => $class)
        {
            $this->$var =& load_class($class);
        }

I believe the problem is that "$this->$var" is assigned by reference. When I comment out my "__get()" function, everything (except my overloading) works fine!

Does anyone have an idea how this would work?
#2

[eluser]diego6616[/eluser]
You should have a fallback to the original object properties, if you could not find the property on the array. This can have some rare behavior if someone defines a GET value with the same name as a, for example, library loaded by CI. If it is on the get_params array, you could not reach the library instance by that mean ($this->library_name). I do not recommend messing with those magic methods on Controllers. It could work inside a library, since you can get the main CI instance by other means (you can also use get_instance() in your controller, but I think it's a messy approach).

Code:
// magic method to set parameter
    function __set($name, $value) {
        if (in_array($name, $parameters)) { // if it's a valid parameter
            $this->get_params[$name] = $value;
        else {
            $this->$name = $value;
        }
    }
    
    // magic method to get parameters
    function __get($name) {
        if (array_key_exists($name, $this->get_params)) {
            return $this->get_params[$name];
        } else {
            return $this->$name;
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB