Welcome Guest, Not a member yet? Register   Sign In
Initiating constructor in my own library class
#1

[eluser]Unknown[/eluser]
As far as i've undertstood you've to load libraries to use custom classes
and so i've a User class which serves as a user object that will be initiated
and stored in my session.

In my login_model when login has been validated i do this:
Code:
$userObj = $this->load->library('user', $data);

(the $data paramter is a result of a db->get->where query that i've done a $row->(); on.)

On this particular row it loads up my User.php which is located in the libraries folder, however it seems to fail passing the data along. In the constructor of the User class it says that the $data parameter is apparently null.

This is how my User class looks:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User
{
  private $m_userId = null;
  private $m_username = null;
  private $m_password = null;
  
  public function __construct($data)
  {  
   $this->m_userId = $data->id;
   $this->m_username = $data->username;
   $this->m_password = $data->password;
  }
  
  public function GetUserId()
  {
   return $this->m_userId;
  }
  
  public function GetUser()
  {
   return $this->m_username;
  }

  public function GetPassword()
  {
   return $this->m_password;
  }
  
}
#2

[eluser]jwindhorst[/eluser]
I ran into a similar issue and found it annoying that I had to return data from the model back to the controller before I could use the data to instantiate my own libraries. My solution was to add an object funtion to my custom MY_Loader file inside the application/core directory. Here is the function:
Code:
public function object($objectName, $params = false)
    {
        // Grab the super object
        $CI =& get_instance();

        // Grab the custom class path
        $customClassFile = getcwd() . $CI->config->item('custom_class_path') . ucfirst($objectName) . '.php';

        // Require the class file once
        require_once($customClassFile);

        // Fix the capitalization, and instantiate the object;
        $realClassName = ucfirst($objectName);
        $obj = new $realClassName($params);

        // return the instantiated object.
        return $obj;
    }

Essentially this simply overloads the Loader to allow you to return instantiated objects. It relies on an additional parameter being added to the config file which arguably could have been placed in a separate custom config but since it's the only parameter I needed I left it in the default.
Code:
/*
|--------------------------------------------------------------------------
| Custom Class Path
|--------------------------------------------------------------------------
|
| File path to where you would like to store your custom object classes
| WITH a trailing slash:
|
| /application/classes/
|
*/
$config['custom_class_path'] =  '/application/classes/';

In this case I called the directory classes but clearly it's arbitrary.

Having this in place, inside of a model I can sa:
Code:
if($pQuery->num_rows() > 0)
{
    foreach($pQuery->result() as $row)
    {          
        $projects[] = $this->load->object('project', $row);
    }
}

This allows me to return a list of instantiated objects from my models (in this case Projects.getProjects) to my controllers thus keeping the controllers cleaner and abstracting off the logic in a better way. IMHO of course.

Hope it helps.




Theme © iAndrew 2016 - Forum software by © MyBB