我有以下PHP5代码:
$request = NULL;
$request->{"header"}->{"sessionid"}        =  $_SESSION['testSession'];
$request->{"header"}->{"type"}             =  "request";第2行和第3行产生以下错误:
严格标准:从空值创建默认对象
如何修复此错误?
发布于 2009-12-23 07:57:34
Null不是一个对象,所以你不能给它赋值。从您正在做的事情看,您似乎需要一个associative array。如果你一心想要使用对象,你可以使用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";我建议使用数组,因为它是一种更简洁的语法,(可能)具有相同的底层实现。
发布于 2009-12-23 07:58:19
去掉$request = NULL,替换为:
$request = new stdClass;
$request->header = new stdClass;您正在尝试写入NULL,而不是实际的对象。
发布于 2009-12-23 07:58:59
要抑制错误,请执行以下操作:
error_reporting(0);要修复错误,请执行以下操作:
$request = new stdClass();hth
https://stackoverflow.com/questions/1949966
复制相似问题