I have a class that has a method that expects a response from an API service in array format. This method then converts the response array into an object by casting (object)$response_array. After this the method attempts to parse the contents of the object. There is a possibility that the returned array could be empty. Before parsing the contents of the object in my class method, I perform a check for null or empty object in an if...else block. I would like to use an equivalence comparison operator like if($response_object === null){} and not if(empty($response_object)){}.
Below is how my class looks like
<?php
class ApiCall {
//this method receives array response, converts to object and then parses object
public function parseResponse(array $response_array)
{
$response_object = (object)$response_array;
//check if this object is null
if($response_object === null) //array with empty content returned
{
#...do something
}
else //returned array has content
{
#...do something
}
}
}
?>
So my question is - is this the right way to check for empty object, without using the function empty() and is it consistent? If not then how can I modify this code to get consistent results. This would help me know if null and empty mean the same thing in PHP objects. I would appreciate any answer where I can still use an equivalent comparison like this ===
It is not the right way to check for an empty object. If you call your function parseResponse with an empty array, the if condition will still be false.
So, if you would put echo in the if-else code like this:
class ApiCall {
//this method receives array response, converts to object and then parses object
public function parseResponse(array $response_array)
{
$response_object = (object)$response_array;
//check if this object is null
if($response_object === null) { // not doing what you expect
echo "null";
}
else {
echo "not null";
}
}
}
Then this call:
ApiCall::parseResponse(array()); // call with empty array
... will output
not null
The same happens if you test for empty($response_object). This used to be different in a distant past, but as from PHP 5.0 (mid-2004), objects with no properties are no longer considered empty.
You should just test on the array you already have, which is falsy when empty. So you can just write:
if(!$response_array) {
echo "null";
}
else {
echo "not null";
}
Or, if you really want an (in)equality, then do $response_array == false, making sure to use == and not ===. But personally, I find such comparisons with boolean literals nothing more than a waste of space.
All of the following would be working alternatives for the if condition:
Based on $response_array:
!$response_array
!count($response_array)
count($response_array) === 0
empty($response_array)
Based on $response_object:
!get_object_vars($response_object)
!(array)($response_object)
Note that get_object_vars could give a different result than the array cast method if $response_object were not a standard object, and would have inherited properties.
Look at this example
$ php -a
php > $o = (object)null;
php > var_dump($o);
class stdClass#2 (0) {
}
php > var_dump(!$o);
bool(false)
So, it is not good idea to compare object with null in your case. More about this: How to check that an object is empty in PHP?
Related
discussing with a friend of my work, we discover something weird about PHP. Let's get the following code:
<?php
$leo = false;
$retorno = $leo[0];
var_dump($retorno);
The return of var_dump() is NULL. Now, the thing is, why is returning NULL, if we are trying to access a bool as array?
The correct behavior isn't throw an exception telling us, that we are trying to access a non-array object as array (in this case a boolean var)?
what you guys think about that?
Since you are trying to access not a string, but a boolean it returns NULL. As from the manual:
Note:
Accessing variables of other types (not including arrays or objects implementing the appropriate interfaces) using [] or {} silently returns NULL.
It's NULL because $leo[0] isn't $leo. You haven't assigned the bool or string to $leo[0], therefore it's empty, and ultimately results in being NULL.
If you were to put:
$retorno = $leo;
Or
$leo[0] = false;
Then you would get the result you are expecting.
$leo = false;
$retorno = array($leo);
var_dump($retorno[0]);
Try this
I have found there to be multiple ways to check whether a function has correctly returned a value to the variable, for example:
Example I
$somevariable = '';
$somevariable = get_somevariable();
if ($somevariable)
{
// Do something because $somevariable is definitely not null or empty!
}
Example II
$somevariable = '';
$somevariable = get_somevariable();
if ($somevariable <> '')
{
// Do something because $somevariable is definitely not null or empty!
}
My question: what is the best practice for checking whether a variable is correct or not? Could it be different for different types of objects? For instance, if you are expecting $somevariable to be a number, would checking if it is an empty string help/post issues? What is you were to set $somevariable = 0; as its initial value?
I come from the strongly-typed world of C# so I am still trying to wrap my head around all of this.
William
It depends what you are looking for.
Check that the Variable is set:
if (isset($var))
{
echo "Var is set";
}
Checking for a number:
if (is_int($var))
{
echo "Var is a number";
}
Checking for a string:
if (is_string($var))
{
echo "var is a string";
}
Check if var contains a decimal place:
if (is_float($var))
{
echo "Var is float";
}
if you are wanting to check that the variable is not a certain type, Add: ! an exclamation mark. Example:
if (!isset($var)) // If variable is not set
{
echo "Var Is Not Set";
}
References:
http://www.php.net/manual/en/function.is-int.php
http://www.php.net/manual/en/function.is-string.php
http://www.php.net/manual/en/function.is-float.php
http://www.php.net/manual/en/function.isset.php
There is no definite answer since it depends on what the function is supposed to return, if properly documented.
For example, if the function fails by returning null, you can check using if (!is_null($retval)).
If the function fails by returning FALSE, use if ($retval !== FALSE).
If the function fails by not returning an integer value, if (is_int($retval)).
If the function fails by returning an empty string, you can use if (!empty($retval)).
and so on...
It depends on what your function may return. This kind of goes back to how to best structure functions. You should learn the PHP truth tables once and apply them. All the following things as considered falsey:
'' (empty string)
0
0.0
'0'
null
false
array() (empty array)
Everything else is truthy. If your function returns one of the above as "failed" return code and anything else as success, the most idiomatic check is:
if (!$value)
If the function may return both 0 and false (like strpos does, for example), you need to apply a more rigorous check:
if (strpos('foo', 'bar') !== false)
I'd always go with the shortest, most readable version that is not prone to false positives, which is typically if ($var)/if (!$var).
If you want to check whether is a number or not, you should make use of filter functions.
For example:
if (!filter_var($_GET['num'], FILTER_VALIDATE_INT)){
//not a number
}
I am returning a json_encode() of an array of objects pulled from an ORM. It includes lots of properties with a null value. What is the neatest way to remove these properties that are null? I guess I could iterate over the properties, look if they are null and then unset() that property, but surely there must be a more elegant way?
Try this; it will only work on a simple object, but if it's coming from an ORM it should be simple enough.
// Strips any false-y values
$object = (object) array_filter((array) $object);
Thanks to Gordon's answer to another question yesterday for giving me the idea.
This works by
converting the object to an associative array, where object properties are the keys and their values are the array values
using array_filter with default arguments to remove array entries with a false (e.g. empty, or null) values
converting the new array back to a simple object
Note that this will remove all properties with empty values, including empty strings, false boolean values and 0s, not just nulls; you can change the array_filter call if you want to keep those and only remote those that are exactly null.
// Strips only null values
$object = (object) array_filter((array) $object, function ($val) {
return !is_null($val);
});
I'm going to add to the response given by El Yobo because that will only work if you have a 1 dimensional object or array. If there is any array or object nesting then in order to get the accepted solution to work you must create some sort of recursive array filter. Not good.
The best solution my colleague and I came up with was to actually perform a regular expression on the JSON string before it was returned from the server.
$json = json_encode($complexObject);
echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $json);
The regular expression will remove all places in the string of the form ,"key":null including any whitespace between the leading comma and the start of the key. It will also match "key":null, afterwards to make sure that no null values were found at the beginning of a JSON object.
This obviously isn't the most ideal situation but it will effectively remove null entries without having to develop some kind of recursive array filter.
Despite the name you can also use array_walk with a closure:
// Setup
$obj = (object) array('foo' => NULL, 'bar' => 'baz');
// equivalent to array_filter
array_walk($obj, function($v,$k) use ($obj) {
if(empty($v)) unset($obj->$k);
});
// output
print_r($obj);
Output
stdClass Object
(
[foo] => bar
)
There's no standard function to remove null-valued properties. Writing one of your own isn't inelegant, if you write one elegantly.
I made this function that solves my problem: clean null 'object properties' and 'array properties' inside an object. Also, you can have many 'levels' of objects/arrays inside of an object:
function cleanNullsOfObject(&$object) {
foreach ($object as $property => &$value) {
if (is_object($value)) {
cleanNullsOfObject($value);
if (empty(get_object_vars($value))) {
unset($object->$property);
}
}
if (is_array($value) && is_object($value[0])) {
foreach ($value as $val) {
cleanNullsOfObject($val);
}
}
if (is_null($value) || (is_array($value) && empty($value))) {
unset($object->$property);
}
}
}
//test
$object = new stdClass();
$object->property = "qwe";
$object->nullProperty = null;
$propertyObject = new stdClass();
$propertyObject->property = "asd";
$propertyObject->nullProperty = null;
$object->propertyObject = $propertyObject;
var_dump($object);
echo "<br>";
echo "<br>";
cleanNullsOfObject($object);
var_dump($object);
Building off of #Gordon 's answer, a couple of adjustments would be needed to make that work, but you can also use array_walk_recursive instead. The reference is required or otherwise any changes you make to the object won't apply to the scope outside of the Closure.
IE:
$someObject = (array)$someObject;
array_walk_recursive($someObject, function($v,$k) use (&$someObject) {
if($someObject[$k] == null) {
unset($someObject[$k]);
}
});
$someObject = (object)$someObject;
var_dump($someObject);
Is there a way to check if an object has any fields? For example, I have a soap server I am querying using a soap client and if I call a get method, I am either returned an object containing fields defining the soap query I have made otherwise I am returned object(stdClass)#3 (0) { }.
Is there a way to tell if the object has anything?
public function get($id){
try{
$client = new soapclient($this->WSDL,self::getAuthorization());
$result = $client->__soapCall('get', array('get'=> array('sys_id'=>$id)));
if(empty($result)){$result = false; }
}catch(SoapFault $exception){
//echo $exception;
$result = false;
}
return $result;
}//end get()
This method should return either an object or false and I am only receiving an object with no fields or an object with fields.
Updated to reflect current behavior, 5/30/12
empty() used to work for this, but the behavior of empty() has changed several times. As always, the php docs are always the best source for exact behavior and the comments on those pages usually provide a good history of the changes over time. If you want to check for a lack of object properties, a very defensive method at the moment is:
if (is_object($theObject) && (count(get_object_vars($theObject)) > 0)) {
...
One of the user contributed code on the php empty() page which I think addresses your problem of checking if the array is filled but has empty values.
http://www.php.net/manual/en/function.empty.php#97772
To find if an array has nothing but empty (string) values:
<?php
$foo = array('foo'=>'', 'bar'=>'');
$bar = implode('', $foo);
if (empty($bar)) {
echo "EMPTY!";
} else {
echo "NOT EMPTY!";
}
?>
I am working on a signup form, I am using PHP and on my processing part I run some code, if a submitted item fails I then add it to an errors array.
Below is a snip of the code, I am at the point where I need to find the best method to determine if I should trigger an error.
So if there is a value set in the error array then I need to redirect and do some other stuff.
I was thinking of using isset or else is_array but I don't think that is the answer since I set the array using **$signup_errors = array()** wouldn't this make the is_array be true?
Can anyone suggest a good way to do this?
//at the beginning I set the error array
$signup_errors = array();
// I then add items to the error array as needed like this...
$signup_errors['captcha'] = 'Please Enter the Correct Security Code';
if ($signup_errors) {
// there was an error
} else {
// there wasn't
}
How does it work? When converting to boolean, an empty array converts to false. Every other array converts to true. From the PHP manual:
Converting to boolean
To explicitly convert a value to
boolean, use the (bool) or (boolean)
casts. However, in most cases the cast
is unncecessary, since a value will be
automatically converted if an
operator, function or control
structure requires a boolean argument.
See also Type Juggling.
When converting to boolean, the
following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
You could also use empty() as it has similar semantics.
Perhaps empty()?
From Docs:
Return Values
Returns FALSE if var has a non-empty
and non-zero value.
The following things are considered to
be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Check if...
if(count($array) > 0) { ... }
...if it is, then at least one key-value pair is set.
Alternatively, check if the array is not empty():
if(!empty($array)) { ... }
Use array_filter if you already have keys, but want to check for non-boolean evaluated values.
<?php
$errors = ['foo' => '', 'bar' => null];
var_dump(array_filter($errors));
$errors = ['foo' => 'Oops', 'bar' => null];
var_dump(array_filter($errors));
Output:
array(0) {
}
array(1) {
["foo"]=>
string(4) "Oops"
}
Use:
<?php
if(array_filter($errors)) {
// Has errors
}
You could check on both the minimum and maximum values of the array, in this case you can have a large array filled with keys and empty values and you don't have to iterate through every key-value pair
if(!min($array) && !max($array)) { ... }
The language construct isset(), is for testing to see if variables and array elements are set and not NULL. Using is_array() would tell you if the argument you supply to it is an array. Thus, I do not think using isset() or is_array() would give you the correct and desired result that you are seeking.
The code:
$signup_errors = array();
means that ...
is_array($signup_errors);
would return true. However, this does not mean that the Boolean language rules of PHP would evaluate....
if($signup_errors)
{
//*Do something if $signup_errors evaluates to true*;
}
as true, unless some elements are added to it. When you did this,
$signup_errors['captcha'] = 'Please Enter the Correct Security Code';
you fulfilled the PHP language requirement for the array above to evaluate to true.
Now, if for some reason you wanted, or needed, to use isset() on the array elements in the future, you could. But, the conditional statement above is enough for you this case.
I should add an obvious answer here. If you initialise your error array as an empty array. And later want to check if it is no longer an empty array:
<?php
$errors = [];
if($errors !== [])
{
// We have errors.
}