未定义的属性:stdclass但属性确实存在?

时间:2017-05-16 17:34:08

标签: php variables object

当我运行脚本时,收到以下错误消息:

PHP Notice:  Undefined property: stdClass::$sub in /path/to/cron_monitor.php on line 14

第14行是这样的(包括第13行,因为它也是相关的):

$data=(object)$obj;
$subject=$data->sub;

Google /其他堆栈问题告诉我该属性不应该存在,但是如果我var_dump($data),我会得到这个(摘录自较大部分,但$data是从4chan {catalog.json输出的。 1}} API),表明它确实存在:

object(stdClass)#2 (26) {
  ["no"]=>
  int(176833602)
  ["now"]=>
  string(21) "05/15/17(Mon)02:08:45"
  ["name"]=>
  string(9) "Anonymous"
  ["sub"]=>
  string(28) "/dfg/ Dwarf Fortress General"
  ["com"]=>
  string(2173) "(excluded since it just bloats up the question, the string is correct)"
  ["filename"]=>
  string(12) "Human Farmer"
  ["ext"]=>
  string(4) ".jpg"
  ["w"]=>
  int(1530)
  ["h"]=>
  int(1027)
  ["tn_w"]=>
  int(250)
  ["tn_h"]=>
  int(167)
  ["tim"]=>
  float(1494828525011)
  ["time"]=>
  int(1494828525)
  ["md5"]=>
  string(24) "0tPuwatHh8Kq/xHEEaWR2Q=="
  ["fsize"]=>
  int(1205673)
  ["resto"]=>
  int(0)
  ["bumplimit"]=>
  int(0)
  ["imagelimit"]=>
  int(0)
  ["semantic_url"]=>
  string(26) "dfg-dwarf-fortress-general"
  ["custom_spoiler"]=>
  int(1)
  ["replies"]=>
  int(435)
  ["images"]=>
  int(113)
  ["omitted_posts"]=>
  int(432)
  ["omitted_images"]=>
  int(110)
  ["last_replies"]=>
  array(3) {
    [0]=>
    array(6) {
      ["no"]=>
      int(176969172)
      ["now"]=>
      string(21) "05/16/17(Tue)13:14:50"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(171) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494954890)
      ["resto"]=>
      int(176833602)
    }
    [1]=>
    array(6) {
      ["no"]=>
      int(176969476)
      ["now"]=>
      string(21) "05/16/17(Tue)13:18:23"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(124) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494955103)
      ["resto"]=>
      int(176833602)
    }
    [2]=>
    array(6) {
      ["no"]=>
      int(176969731)
      ["now"]=>
      string(21) "05/16/17(Tue)13:21:20"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(179) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494955280)
      ["resto"]=>
      int(176833602)
    }
  }
  ["last_modified"]=>
  int(1494955280)
}

奇怪的是,我后来还有一行

$threadno=$data->no;

这确实会返回有效值(整数)。

编辑:我的整个代码块:

<?php
error_reporting(E_ALL);
include "chan_archiver.php";
include "config.php";

class threadMonitor extends chan_archiver{
        function monitorCatalog($boardwatch, $filter, $basedescription) {
                $json=json_decode( file_get_contents('http://a.4cdn.org/'.$boardwatch.'/catalog.json'),true);
                $monitordescription=$basedescription.date(DATE_RFC850);
                foreach( $json as $thread ){
                        $arr=$thread['threads'];
                        foreach( $arr as $obj ){
                                $data=(object)$obj;
                                var_dump($data);
                                $subject=$data->sub;
                                if (strpos($subject, $filter) !== false){
                                                $threadno=$data->no;
                                                $this->addThread($threadno, $boardwatch, $monitordescription);
                                }
                        }
                }
        }
}
$t=new threadMonitor();

/* IMPORTANT: Add arguments like this:
  board filter basedescription
  Example crontab command (that last space is needed, the command automatically adds the added date and time to the thread):
        php /path/to/cron_monitor.php vg "/dfg/" "Dwarf Fortress General - "
*/

$t->monitorCatalog($argv[1],$argv[2],$argv[3]);
?>

1 个答案:

答案 0 :(得分:0)

您的数据结构不够深入

看起来sub并不总是存在于所有出现中,这可能就是你收到错误的原因

foreach( $json as $obj ){

    foreach ( $obj->threads as $thread ) {
        echo $thread->no . PHP_EOL;
        echo $thread->now . PHP_EOL;
        if ( isset($thread->sub) ) {
            echo $thread->sub . PHP_EOL;
        }
    }

}