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?
#6

[eluser]mrking[/eluser]
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_lib {

    function User_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) {//$params[0] - because[0] is the object that contains the array of information, print_r($param)
            $this->initialize($param);
        }
        
        log_message('debug', 'Review_lib class initialized');
    }
    
    function initialize($param = array()) {
        if(count($param) > 0) { //initialise the parameters
            //initialise the class parameters
            foreach ($param as $key => $val) {
                $this->$key = $val;
            }
            
            //initialise the field in db to become local variable
            $this->user_info = $this->user_info();
            
        } else {
                log_message('debug', 'User_lib param failed to initialized.');
                exit;
        }
    }
    
    function user_info() {
        $this->ci->load->model('user/user_model');

        if($user_info = $this->ci->user_model->user_info($this->username)) { //username exist and array containing all user info is returned
            return $user_info; //returns an object from user_model as result will be a single row
        } else { //product id not found, redirect to gallery
                log_message('debug', 'username not found');
                return FALSE;
        }
    }
}

this is the edited one where each call from the loop creates a new instance.

so the logic is this, there are 2 products(contain in an array) by 1 user. so, foreach each product, so some processing, return some result and eventually deduct point from user.

so the first loop execution of

Code:
$point_to_update =  $this->ci->[b]user_lib[/b]->user_info->point - $this->point; //deduct point

got no problem, but once it reach the second loop execution of the same code,

Code:
$this->ci->[b]user_lib[/b]->user_info->point

seems not to be updated from the deduction of the first loop.


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:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB