Welcome Guest, Not a member yet? Register   Sign In
How to instantiating a object
#1

[eluser]psycho-vnz[/eluser]
Hi, i have few time using CI and i have my first doubt.
How i can instantiating a class that represent a entity made by me?
Example:
Code:
class Man
{
    $name = 'psycho';
    
    function Man()
    {
        
    }
    
    function get_name()
    {
        return $this->name;
    }
}
i want do something like this in a Model
Code:
$man = new Man();
$man->get_name();
The class Man should extend from a Model?
Where i should put the file of the class Man? Model, scripts, library?
Thanks.
#2

[eluser]Pascal Kriete[/eluser]
Where you put it and how you use it depends very much on what the class does.

If you stick it in the libraries folder, using $this->load->library('man') will include and instantiate it for you. If you need another instance of the same class, it's already included so you can just use the 'new' operator (my personal experience is that this is rarely needed though).

Welcome to CodeIgniter.
#3

[eluser]psycho-vnz[/eluser]
Thanks inparo,

How i can do to get or set values of the instance of teh object 'man', to be returned to the view 'man_view' and get the values of that object?
#4

[eluser]Michael Wales[/eluser]
model:
Code:
class Man extends Model() {

  var $params;  

  function Man() {
    parent::Model();
    $params = array();
  }

  function set($key, $val) {
    $this->params[$key] = $val;
  }

  function get($key) {
    return $this->params[$key]
  }
}

Controller:
Code:
class Home extends Controller {
  function Home() {
    parent::Controller();
  }

  function index() {
    $m = new Man();
    $m->set('name', 'Michael Wales');
    $this->load->view('view', $m);
  }

  function another_way() {
    $m = new Man();
    $m->set('name', 'Michael Wales');
    $data['name'] = $m->get('name');
    $this->load->view('view', $data);
  }
}

View:
Code:
<?php echo $name; ?>
#5

[eluser]psycho-vnz[/eluser]
Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB