views:

62

answers:

2

Hello,

I would like to know what is the best practice to define class members in a CodeIgniter model?

Thank you.

A: 

http://codeigniter.com/wiki/Calling_CI_models_from_outside_script/

Vinothbabu
It's not what I asked.
thedp
+2  A: 

You can place member variables and funciton in a model like in any php class.

A very simple example of a CI Model:

class sample_model extends Model {
    // private member variable
    private var $sample_arr;

    // public constructor
    public function __construct()
    {
        // call parent constructor
        parent::__construct();
        // init sample array
        $sample_arr = array();
    }
}

More Info about classes in php http://www.php.net/manual/en/language.oop5.basic.php

redimp
So basically declare them outside the constructor and assign values in the constructor.
thedp
CodeIgniter Models are just PHP classes, so define class variables the same as you do in PHP. You define them before the first class method and use them wherever and however you like. You can define members with literal strings, ints, floats, arrays, etc in outside of a method, but to define them with dynamic values they must be in a method (the construct is one of the methods).
Phil Sturgeon