简体   繁体   English

PHP注意:尝试获取非对象的属性

[英]PHP Notice: Trying to get property of non-object

When I get file content from javascript, decode it and get the value of object. 当我从javascript获取文件内容时,对其进行解码并获取对象的值。 It display trying to get property of non-project. 显示试图获取非项目的属性。 However, the object variable is successfully pass to another php class. 但是,对象变量已成功传递给另一个php类。 May I know why it cause this problem? 我可以知道为什么会导致这个问题吗? I had try all the solution from stackoverflow but it not work and it also display the same error. 我已经尝试了所有来自stackoverflow的解决方案,但是它不起作用,并且还会显示相同的错误。 The following is my code: 以下是我的代码:

$chat_info = file_get_contents("php://input");
$chat_request = json_decode($chat_info);
$username = $chat_request->username;
$messageContent = $chat_request->messageContent;
$dateTime = $chat_request->date_time;
$channel = $chat_request->channel;
$event = $chat_request->event;

You can also do this: 您也可以这样做:

$chat_info = file_get_contents("php://input");
$chat_request = json_decode($chat_info,true);
$username = $chat_request['username'];
$messageContent = $chat_request['messageContent'];
$dateTime = $chat_request['date_time'];
$channel = $chat_request['channel'];
$event = $chat_request['event'];

And after it if it show any error like undefined index then the $chat_info variable does not contain the value of a key that you want. 然后,如果它显示任何错误( $chat_info undefined index$chat_info变量将不包含所需键的值。

Probably one of the object which you use get from time to time NULL value. 您使用的对象之一可能会不时获得NULL值。 Check this out, i hope that is correct answer. 检查一下,我希望这是正确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM