Welcome Guest, Not a member yet? Register   Sign In
Messing with OOP in Codeigniter
#1

[eluser]fran-quero[/eluser]
Hello I have been some time working with CI (awesome framework!), and now I'm reading about OOP in order to create my own libraries. Theory is almost clear, but practice is another song Smile

I'm creating a test library, but I don't know how to an object instance after sending a form or moving between a controller methods. I tried both with the same result.

Please, take a look to my code and feel free to give me all advices and corrections as necessary:

My Class
Code:
class FqGallery
{
public $confirmation_type;
public $confirmation_url;

public function __construct()
{
  $this->load->database();
  $this->load->model('m_fqgallery');
  $this->load->library('form_validation');
  $this->load->library('session');
}

        public function __get($var) //Copied from ion_auth
{
  return get_instance()->$var;
}

        public function deleteGallery($id)
{
  if($this->confirmation_type == 'deleteGalery')
  {
   //Code to delete the galery
  } else {
   $this->confirmation_type = 'deleteGalery';
   $this->confirmation_url = current_url();
  
                        return false;
  }
}

Controller:
Code:
class Gallery extends CI_Controller
{
public $gallery;

public function __construct()
{
  parent::__construct();
  $this->load->library('FqGallery');
  $this->gallery = new fqGallery();
}

        public function delete($id)
{
  if(!$this->gallery->deleteGallery($id))
  {
   $data['confirmation_message'] = 'Do you really want to delete this gallery?';
          $this->load->view('fqGallery/v_confirmation',$data);
  } else {
                      //Show success message
                }
}

        public function confirm()
        {
              /*
                *  Here is the problem. I always have this property empty.
                *  If I define a default value in the class when I declare the property,
                *  here I receive that default value.
                */
              echo $this->gallery->confirmation_type;
              //I know I should use setters and getters, just to simplify my post.
        }

The view
Code:
echo $confirmation_message;
echo anchor('gallery/confirm','I am sure');

I can bypass this issue using session flashdata, but I'd like to know the correct way to work with object instances.

Thanks in advance for your help and advices!


Messages In This Thread
Messing with OOP in Codeigniter - by El Forum - 02-10-2013, 05:34 AM
Messing with OOP in Codeigniter - by El Forum - 02-10-2013, 12:38 PM
Messing with OOP in Codeigniter - by El Forum - 02-11-2013, 12:40 AM
Messing with OOP in Codeigniter - by El Forum - 02-11-2013, 05:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB