我有一个类似的JSON文件:
{
"product": "www.example.com",
"enabled": true,
...
}
使用 file_get_contents()和 json_decode($ string,FALSE)后,我得到了一个带有属性的经典stdClass Object
。 Var_dump()
返回以下输出:
object(stdClass)#2 (7) {
["product"] => string(15) "www.example.com"
["enabled"] => bool(true)
...
}
然后,事情变得奇怪。
在99%的情况下,$myobject->product
会返回字符串“www.example.com”。这就是你的期望,对吧?
但有时(平均:我们的生产服务器每分钟2次),$myobject->product
会返回NULL
。 我无法解释。
我在该行之前和之后添加了一些调试,这是我在出现错误行为时得到的:
var_dump($myobject) : gives the output given before
print_r($myobject) : OK
var_export($myobject) : OK
is_object($myobject) : TRUE
isset($myobject->product) : TRUE
is_string($myobject->product) : FALSE !!!
gettype($myobject->product) : NULL !!!
get_object_vars($myobject) : Array('product'=>'www.example.com', 'enabled'=>true, ...)
get_class_methods($myobject) : Array()
ReflectionClass::export($myobject) : Class [ <internal:Core> class stdClass ] (0 constant, 0 property, 0 method, ...)
我通过Apache运行PHP代码。我没有Apache错误,没有PHP错误,没有。当我尝试访问它时,它只是返回null
的属性,但是当我在代码之前和之后转储变量对象时,它显然存在。
phpinfo()
)提前致谢!
PS:由于我公司的政策,我无法提供确切的文件和代码,但我很乐意回答您的所有问题。
使用您可以测试的PHP代码进行更新:
<?php
// Show me all errors
ini_set("display_errors",1);
error_reporting(-1);
// Initialize the object
$object = json_decode('{"product": "www.example.com"}', false);
echo "\n *** DUMP *** \n";
var_dump($object);
echo "\n Product : ";
echo $object->product;
echo "\n Is the variable an object ? ".(is_object($object) ? "Yes" : "No");
echo "\n Is the property set ? ".(isset($object->product) ? "Yes" : "No");
echo "\n Is the property a string ? ".(is_string($object->product) ? "Yes" : "No");
echo "\n Is the property null ? ".(is_null($object->product) ? "Yes" : "No");
echo "\n END";
预期的结果,我获得了98/100的Apache请求
HTTP/1.1 200 OK
Date: Thu, 23 Jan 2014 17:18:37 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.14
Vary: Accept-Encoding
Content-Length: 241
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
Accept-Ranges: none
*** DUMP ***
object(stdClass)#1 (1) {
["product"]=>
string(15) "www.example.com"
}
Product : www.example.com
Is the variable an object ? Yes
Is the property set ? Yes
Is the property a string ? Yes
Is the property null ? No
END
越野车的结果我也得到了......
HTTP/1.1 200 OK
Date: Thu, 23 Jan 2014 17:18:37 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.14
Vary: Accept-Encoding
Content-Length: 580
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
Accept-Ranges: none
*** DUMP ***
object(stdClass)#1 (1) {
["product"]=>
string(15) "www.example.com"
}
Product :
Notice: Trying to get property of non-object in /var/[...]/test_object.php on line 12
Is the variable an object ? Yes
Is the property set ? Yes
Notice: Trying to get property of non-object in /var/[...]/test_object.php on line 15
Is the property a string ? No
Notice: Trying to get property of non-object in /var/[...]test_object.php on line 16
Is the property null ? Yes
END
答案 0 :(得分:0)
对我来说似乎是一个PHP内部错误,但是,要修复它,你应该首先通过生成能够重现问题的最小脚本来识别真正的问题,例如,脚本,如一个下面,在生产服务器加载时重现问题?:
$object = json_decode('{"product": "www.example.com"}', false);
echo $object->product;
如果 ,请使用脚本和相关信息在https://bugs.php.net/创建一个PHP错误报告。如果不,则表示您必须通过以下操作增加脚本,其操作与现实应用程序中的操作类似:
file_get_contents()
直到您能够使用小片段重现错误。
最后,创建错误报告,其中包含使其可重现性所需的内容,您可能会发现,同时问题是由一些无效数据(格式错误的json?错误的unicode序列?...)引起的,甚至可能可按需复制。
请注意,StackOverflow并不适合调查错误。
答案 1 :(得分:0)
查看你的脚本,你正在使用的PHP和APC版本,它显然不是PHP问题,而是APC版本,升级它,你的PHP也是如此可能,你的bug几乎可以肯定是清漆!
答案 2 :(得分:0)
经过深入调查后,我们得出结论,PHP 5.3.2中存在一个错误,阻止从stdClass继承的所有对象访问属性。变量“globale std_object_handlers”已损坏。 这正是这里描述的内容:https://bugs.php.net/bug.php?id=50027#1289900303(后面的消息中的修订号可能是错误的)
答案 3 :(得分:0)
升级到 PHP 5.3.10 (Ubuntu 12.04附带)修复了PHP错误。