3

I would like to check if a PHP object has a property. For PHP5 I could use

if (property_exists($object, "foo")) {...}

but how can I do the same in PHP4?

4

2 回答 2

8

如果您的属性存在,您可以使用get_object_vars并检查结果数组。

请参阅get_object_vars此处的文档:http: //php.net/manual/en/function.get-object-vars.php

于 2013-07-04T06:47:38.633 回答
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;
}
于 2014-05-08T02:06:07.880 回答