0

I've got a problem when assigning data to an object variable. In all my time, i haven't seen such weird behaviour.

I have a class:

class Main extends CI_Controller {

    public $data = array();

    public function __construct(){
        parent::__construct();
        /**
         * Load Models
         */
        $this->load->model('rss');
        $this->load->model('model_product');

        /**
         * Default loaded Data
         */
                $this->data['newsfeed'] = $this->rss->load($this->config->item('newsfeed'));
        }
}

You see that i want to add the newsfeed to the $this->data object. It will held in a key named newsfeed. But now, i want this newsfeed on different pages. It is useless and inefficient to load this on every page, so i extended my main controller.

class Shop extends Main {

    public function __construct(){
        parent::__construct();
    }


    public function index()
    {
        $this->data['content'] = 'shop/default';
        $this->load->view('template/index', $this->data);
    }

    public function product($id){
        $this->data['product'] = $this->model_product->getProducts(null, $id);

        if($this->data['product']->categoryid != $this->config->item('gamecategory')){
            redirect('');
        }

        var_dump($this->data['product']);

        $this->data['content'] = 'shop/product';
        $this->load->view('template/index', $this->data);
    }
}

The expected behaviour would be that $this->data would hold an array of objects (in each key ) but when i var_dump in the shop controller it gives me the entire $data object as return data, instead of the provided key. This totally blows my mind and i cant seem to understand this weird behaviour.

Expected var_dump ($this->data['product']):

Array(x){
 key: value,
 key: value,
 .....
}

actual result:

Object{
     newsfeed:
         - data
     product:
         - data
     ....
     ....
}

Model rss:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Rss extends CI_Model{
        public function __construct(){
            parent::__construct();
        }

        public function load($item){
            return ($xml = simplexml_load_string(file_get_contents($item))) ? $xml : false;
        }
    }
4

0 回答 0