CodeIgniter - Creating default object from empty value

12,383

I believe you want something like this:

$list = array();
$list[0] = new stdClass;
$list[0]->title = "first blog title";
$list[0]->author = "author 1";
$list[1] = new stdClass;
$list[1]->title = "second blog title";
$list[1]->author = "author 2";

But why not using the array as an array?

$list = array();
$list[0]['title'] = "first blog title";
$list[0]['author'] = "author 1";
Share:
12,383

Related videos on Youtube

Nisar ahmed
Author by

Nisar ahmed

Updated on June 04, 2022

Comments

  • Nisar ahmed
    Nisar ahmed almost 2 years
    A PHP Error was encountered
    
    Severity: Warning
    
    Message: Creating default object from empty value
    
    Filename: models/Modeltest.php
    
    Line Number: 13
    

    I am trying to create an array in model and returning it to the controller but it is giving this warning ? Can any body help me out how to resolve it ?

    My ModelClass Code

        $list = Array();
        $list[0]->title = "first blog title";
        $list[0]->author = "author 1";
    
        $list[1]->title = "second blog title";
        $list[1]->author = "author 2";
    
        return $list;
    

    My Contoller Class Code

        $this->load->model("modeltest");
        print_r($this->modeltest->get_articles_list());
    
    • Damien Pirsy
      Damien Pirsy over 9 years
      $list = Array(); $list is...an array, but you're assigning it object properties
    • Nisar ahmed
      Nisar ahmed over 9 years
      then shouldn't it be an error ? but this code works and I have just followed some tutorial ? codesamplez.com/development/codeigniter-basic-tutorial
    • Damien Pirsy
      Damien Pirsy over 9 years
      Yes, it's an error, and you posted it right in the question
    • Shaiful Islam
      Shaiful Islam over 9 years
      @Nisarahmed who wrote that tutorial?It seems he did not tested the code.
  • Nisar ahmed
    Nisar ahmed over 9 years
    Thankssss alott :) This works ;) wait 7 mins till i can accept it as answer
  • Kyslik
    Kyslik over 9 years
    This one is good read if interested array vs stdClass.