我想检查一个PHP对象是否有属性。对于PHP5,我可以使用
if (property_exists($object, "foo")) {...}
但是我怎么能在PHP4中做同样的事情呢?
答案 0 :(得分:8)
如果您的属性存在,您可以使用get_object_vars
并检查生成的数组。
请参阅get_object_vars
此处的文档:http://php.net/manual/en/function.get-object-vars.php
答案 1 :(得分:3)
/***
Just as you would do for PHP5, you can apply the same concept for PHP 4.
***/
//$data is a JSON object am receiving from a server.
$data = json_decode(stripslashes($_REQUEST['data']));
/****
Applying the same property_exists method but remember to keep both object/class
and the property to check for in single quotes.
*****/
if (property_exists('data', 'content'))
{
$content = $data->content;
}