如何解决PHP Strict错误“从空值创建默认对象"? [英] How do I fix the PHP Strict error "Creating default object from empty value"?

查看:79
本文介绍了如何解决PHP Strict错误“从空值创建默认对象"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP5代码:

I have the following PHP5 code:

$request = NULL;
$request->{"header"}->{"sessionid"}        =  $_SESSION['testSession'];
$request->{"header"}->{"type"}             =  "request";

第2行和第3行产生以下错误:

Lines 2 and 3 are producing the following error:

PHP严格标准:从空值创建默认对象

PHP Strict standards: Creating default object from empty value

如何解决此错误?

推荐答案

Null不是对象,因此无法为其分配值.从您的操作看来,您需要一个关联数组.如果您对使用对象一无所知,则可以使用 stdClass

Null isn't an object, so you can't assign values to it. From what you are doing it looks like you need an associative array. If you are dead set on using objects, you could use the stdClass

//using arrays
$request = array();
$request["header"]["sessionid"]        =  $_SESSION['testSession'];
$request["header"]["type"]             =  "request";

//using stdClass
$request = new stdClass();
$request->header = new stdClass();
$request->header->sessionid        =  $_SESSION['testSession'];
$request->header->type             =  "request";

我建议使用数组,因为它是一种更整洁的语法,具有(可能)相同的基础实现.

I would recommend using arrays, as it is a neater syntax with (probably) the same underlying implementation.

这篇关于如何解决PHP Strict错误“从空值创建默认对象"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆