• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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!

#2
[eluser]rjsmith[/eluser]
First, since you want to use CI resources in your library, you have to grab a reference to CI. For example, here's what I would do:
Code:
class FQGallery{
    private $CI;
    //Other member variables
    ...

    public function __construct($options){
        $this->CI =& get_instance();

        //Set member variables using $options array
        ...

        $this->CI->load->database();
        $this->CI->load->model('m_fqgallery');
        $this->CI->load->library(array('form_validation', 'session'));
    }

    ...
}

I would also return 'this' in my methods in case I wanted methods to be chainable.

Next, in the controller:
Code:
class Gallery extends CI_Controller{
    public function __construct(){
        parent::__construct();
        
        $options = array(
            'setting1' => 'value1',
            'setting2' => 'value2'
            ...
        );
        $this->load->library('fqgallery', $options, 'gallery'); //$this->gallery now refers to fqgallery
    }
}

#3
[eluser]Aken[/eluser]
[quote author="rjsmith" date="1360525099"]First, since you want to use CI resources in your library, you have to grab a reference to CI.[/quote]
They did that already through the __get() method.

Unless you need to create multiple instances of an object, your library is already instantiated once when you load it.

Code:
$this->load->libary('fqgallery');
// New instance of gallery object located at:
$this->fqgallery

#4
[eluser]rjsmith[/eluser]
I didn't know __get() would work in the constructor


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.