Welcome Guest, Not a member yet? Register   Sign In
Cannot access class variables
#9

[eluser]chuckl[/eluser]
If that is the case then please explain this real world example, based on an application I am currently developing.
Code:
class Browse extends CI_Controller
{
  private $data = array();

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

  function index()
  {
    // set up some view data
    $this->data['title'] = 'Site Title';
    $this->data['content'] = 'browser/list';
    ...more code
    $this->load->view('includes/template', $this->data);
  }

  function processAjax()
  {
    // Parse data received from AJAX call
    // and save it for later use.
    $this->data['params'] = $this->input->post('item');
  }
  
  function function saveItem()
  {
    $this->load->model('items','items');
    $this->items->save($data['params'];

    $this->load->view('includes/template', $this->data);
  }
}
The processAjax() method is called by a jQuery function passing data via the POST array.

This controller fails in two places.

1. While the processAjax method receives the data off of the POST array and stores in the $data['params'] element, it is only present during the execution of that method. When the method saveItem() is called it is a NEW instance and $data['params'] does not exist.

2. When the view is loaded in the saveItem() method, errors occur because there are unknown variables (e.g. $title, $params) because the call to saveItem() is working on a new and different object instance than when either the index() or processAjax() methods were called; the $data variable is new and an empty array.

The only way (currently) to solve this is to not use the $data variable but store all of the variables that are going to be used by multiple functions within the class in session variables (which are stored in either cookies or the database or both.) If you have more than a few variables that will quickly become expensive processing.


Messages In This Thread
Cannot access class variables - by El Forum - 04-01-2011, 02:42 PM
Cannot access class variables - by El Forum - 04-01-2011, 04:42 PM
Cannot access class variables - by El Forum - 04-01-2011, 05:50 PM
Cannot access class variables - by El Forum - 04-01-2011, 06:22 PM
Cannot access class variables - by El Forum - 04-02-2011, 04:05 PM
Cannot access class variables - by El Forum - 04-02-2011, 05:54 PM
Cannot access class variables - by El Forum - 04-03-2011, 12:22 AM
Cannot access class variables - by El Forum - 04-03-2011, 01:08 AM
Cannot access class variables - by El Forum - 04-03-2011, 01:10 AM
Cannot access class variables - by El Forum - 04-03-2011, 01:34 AM
Cannot access class variables - by El Forum - 04-03-2011, 04:08 AM
Cannot access class variables - by El Forum - 04-03-2011, 06:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB