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?

2 Answers 2

8

You can use get_object_vars and check the resulting array if your property exists.

Please refer to the the documentation on get_object_vars here: http://php.net/manual/en/function.get-object-vars.php

Sign up to request clarification or add additional context in comments.

1 Comment

In the same vein: $obj_arr = (array)$object; print isset($obj_arr["foo"]);
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;
}

1 Comment

Welcome to StackOverflow. Have a look at stackoverflow.com/help/how-to-answer for information about how to write a good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.