Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter: How can I create a new instance of a library whenever the method is called?
#5

[eluser]mrking[/eluser]
ok as mention, my code below. please note that im still new to this, so will be happy to receive any feedback/recommended best practice.


Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Checkout_lib {
    var $error = array();
    var $checkout_id = ''; //initialise once run() is called
    var $total_amount_payable = 0;
    var $type_of_delivery = 'N';
    var $payment_status = '';

    function Checkout_lib($param = array()) {
        $this->ci =& get_instance(); //to grab CI object and insert into this current object so that we can use the CI framework resources, eg. helper etc.
        
        if (count($param) > 0) {
            $this->initialize($param);
        }
    }

    function initialize($param = array()) {
        if(count($param) > 0) { //initialise the parameters
            foreach ($param as $key => $val) {

                switch ($key) { // for the option array in the product array
                    case "options":
                    
                        foreach($val as $option => $val) {
                            $this->$option = $val;
                        }
                        break;
                        
                    case "id": //rename id variable to product_id
                    
                        $this->product_id = $val;
                        break;
                        
                    default: //for the other normal product info such as id, qty, price etc.
                        $this->$key = $val;
                }
            }
        } else {
                log_message('debug', 'Checkout_lib param failed to initialized.');
                exit;
        }
    }
    
    
    function run() {
        foreach($this->checkout_product as $product) {
            $this->initialize($product);
            
            $this->total_amount_payable += $this->price;
            
            if($this->deduct_user_point()
            ) {
                log_message('debug',$this->username.' - '.$this->checkout_id.' - '.$this->product_id.' processed.');
            } else {
                return FALSE;
            }
        }
        if($this->record_payment()) $this->ci->cart->destroy(); else return FALSE;
        return TRUE;
    }
            
    function deduct_user_point() {  //need a new class name for CI to create new instance of object whenever it is called, by default we will use the original file name/class name
        
        $class_name = (isset($this->rowid) ? 'user_lib'.$this->rowid : 'user_lib'); //as we need a unique class name for every call from each product, we will prefix user_lib with $this->rowid to be used as the new class name

        $this->ci->load->library('user_lib',array('username'=>$this->username),$class_name);
        
        $point_to_update =  $this->ci->$class_name->user_info->point - $this->point; //deduct point
        
        if($this->ci->$class_name->update_user_info(array('point'=>$point_to_update))) {
            return TRUE; //point deducted successfully
        } else {
            $this->error['checkout_error'][] = $this->ci->lang->line('error_checkout') .
                                                            $this->ci->lang->line('error_deduct_user_point') .
                                                            $this->ci->lang->line('error_checkout_reference_no') . $this->checkout_id;    
            return FALSE;
        }
    }
}


Messages In This Thread
CodeIgniter: How can I create a new instance of a library whenever the method is called? - by El Forum - 10-07-2009, 04:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB