Welcome Guest, Not a member yet? Register   Sign In
Begginer objects problem
#1

[eluser]zelenkooo[/eluser]
Am total begging and am trying to do very simple thing

here is my model class

Code:
class Person extends CI_Model {

var $name   = '';
  
function __construct()
{
  parent::__construct();
}

function initialise($name)
{
  $this->name = $name;
}

function toString()
{
  return $this->name;
}
}

I have some data as a text , with names age etc in lines, that am parsing. Am parsing data and i will put all data in a collection of Person objects and then later i plan to use some loop , to loop objects and for example calculate average height of person.

1. Is my model class ok ? I will add other attributes later.
2. When i create array and try to add objects do i need to cal load model for every object ?
$this->load->model('Person');
$this->Person->initialise("George");
3. Am i doing something stupid here, cos in Java i would do it like this

thx
#2

[eluser]djuric[/eluser]
I'm not sure why you are doing this in Model, it's more like thing for Controller in my opinion.

Your model looks fine technically, and after you load it with $this->load->model(‘Person’), you are calling it right with $this->Person->initialise(“George”)


#3

[eluser]zelenkooo[/eluser]
Yes am calling that in controller . But when i do this

Code:
$this->load->model(‘Person’);
$this->person->initialise('George');
$array[0]=this->person;
.
.
.
$this->person->initialise('Mark');
$array[1]=this->person;

print_r($arrayOfPersons);
Code:
[0] => Person Object
        (
            [name] => Mark
          
        )

    [1] => Person Object
        (
            [name] => Mark
            
        )
Why do i get 2x Mark when i should get (0)George and (1) Mark ???
#4

[eluser]Aken[/eluser]
Objects are references by default. Updating $this->person makes ALL $this->person references the same thing.

You should instantiate the classes yourself.

Code:
$this->load->model('person');

$person1 = new Person();
$person1->initialise('George');

// etc...




Theme © iAndrew 2016 - Forum software by © MyBB