CodeIgniter - 从空值创建默认对象

时间:2014-11-30 19:57:26

标签: php codeigniter warnings php-5.4 notice

A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: models/Modeltest.php

Line Number: 13

我正在尝试在模型中创建一个数组并将其返回给控制器,但它是否发出此警告?任何人都可以帮我解决问题吗?

我的ModelClass代码

    $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;

我的控制器类代码

    $this->load->model("modeltest");
    print_r($this->modeltest->get_articles_list());

1 个答案:

答案 0 :(得分:8)

我相信你想要这样的东西:

$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";

但为什么不将数组用作数组?

$list = array();
$list[0]['title'] = "first blog title";
$list[0]['author'] = "author 1";