4
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());
4
  • $list = Array(); $list is...an array, but you're assigning it object properties Commented Nov 30, 2014 at 20:00
  • 1
    then shouldn't it be an error ? but this code works and I have just followed some tutorial ? codesamplez.com/development/codeigniter-basic-tutorial Commented Nov 30, 2014 at 20:03
  • Yes, it's an error, and you posted it right in the question Commented Nov 30, 2014 at 20:04
  • @Nisarahmed who wrote that tutorial?It seems he did not tested the code. Commented Nov 30, 2014 at 20:13

1 Answer 1

9

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";
Sign up to request clarification or add additional context in comments.

2 Comments

Thankssss alott :) This works ;) wait 7 mins till i can accept it as answer
This one is good read if interested array vs stdClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.