6

With traditional OOP, i would (or could, rather) create a model / object that represents a User, with properties that reflect that, i.e. name, id, job title etc.

I could then create a new instance of that object and assign it to a variable, and if i was looping through a result set, i could create an instance for each one. With codeigniter, this seems impossible, as doing:

$this->load->model('User');

Instantiates it, and places it for use at $this->user.

Is there no way to use models as objects in a more traditional manner?, but without hacking the CI way of doing things?

I understand that you can assign things to a different object name using $this->load->model('User', 'fubar'), but it's not as dynamic as simply assigning an instance to a variable.

Any insight into the matter is greatly appreciated guys.

EDIT: thanks for the answers guys, i think that i missed a vital part of working the "Codigniter Way", but i've just been looking through a contributed library, and the practice of using the same instance (assigned to the codeigniter namespace) but clearing the instance variables after each use, seems to be a great way to work that removes my reservations.

Once again - thanks for the help and answers.

Dan Matthews
  • 1,245
  • 2
  • 12
  • 24

4 Answers4

5

You don't have to use the load() functions. You can just use require_once and new User(), like you normally would.

Using the load() should only be for things you want in the global CI namespace. As you can see, it gets polluted really quickly if you try to use it for everything.

minboost
  • 2,555
  • 1
  • 15
  • 15
  • Thanks, i guess i knew i could still do this, but wondered if there's something i could do to avoid using require(), and still use CI's loading. I.e. could i do: `$this->load->model('User');` then take copies with `$var = new $this->user` ? – Dan Matthews Dec 05 '11 at 20:19
  • Daniel: see the first item on this article: http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/ – Travis Collins Jan 31 '12 at 01:49
0

You can still instantiate objects in codeigniter the same way as you do it normally.

$user1 = new Users(); // Instantiate
$user2 = new Users();

$user1->property;     // Using it
$user2->method()

unset($user1);        // Unset it when done for good practice.
Anil
  • 21,730
  • 9
  • 73
  • 100
0

Yes, what JustAnil and minboost said are correct, however, standard practice for how the majority of CI developers write code is with the following syntax

$this->mymodel->mymethod(); 

Are you finding a particular problem or issue when you are trying to write like this? Or is it just different from the style you are used to writing in? Please elaborate on a use case where "it's not as dynamic as simply assigning an instance to a variable" :)

-2
// User_model.php

public function get_users() {
    $query = $this->db->get('mytable');
    return $query->result();
}

// User_controller.php
public function show_users() {
    $data['users'] = $this->User_model->get_users();
    $this->load->view('show_users', $data);
}

// show_users.php  (view file)
$x = 0;
foreach ($users as $user) {
    $x = $user;
    $x++;
}
mdgrech
  • 957
  • 2
  • 8
  • 22