I used var_dump on an object in my code. print var_dump( $$user);
result: object(stdClass)#35 (1) { ["user1_ready"]=> string(1) "0" } How do I get to this value (0 in this case).
I tried print $$user which resulted in Catchable fatal error: Object of class stdClass could not be converted to string in
I need something like if($$user == 0){echo "error message") else {
You need to use the property/method accessor "->". It generally follows:
$object->method() or $object->property
so yours would be
$$user->user1_ready
Related
I want to parse this MongoDB error message. I can't manage to access the variables in the object. I assume I overlook something trivial, but I can't figure it out.
The var_dump var_dump($error);
object(MongoDB\Driver\WriteError)#11 (4) {
["message"]=>
string(362) "E11000 duplicate key error collection: database.collection index: c_address collation: { locale: "nl", caseLevel: false, caseFirst: "off", strength: 1, numericOrdering: false, alternate: "non-ignorable", maxVariable: "punct", normalization: false, backwards: false, version: "57.1" } dup key: { address: "0x", city: "0x" }"
["code"]=>
int(11000)
["index"]=>
int(0)
["info"]=>
NULL
}
I try to access message and print it out with echo.
echo $error->message;
Throws: Warning: Undefined property: MongoDB\Driver\WriteError::$message
echo $error["message"];
Throws: Uncaught Error: Cannot use object of type MongoDB\Driver\WriteError as array
The class documentation suggestion from El_Vanja did the trick.
MongoDB\Driver\WriteError has a method [getMessage][1].
My first assumption is to check with var_dump what I get back and it's impossible to see if those variables are protected/private or not (and if they have a function).
I did some more research, converting an object to json and echo that, at least shows which variables are public (in this case none).
echo json_encode($error);
Gives: {}.
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?
Iam getting a boolean from a webservices, var_dump:
object(stdClass)#2 (1) { ["CheckIfUserCridentialsAreValidResult"]=> bool(false) }
i tried to put it in a if statement, like this:
if($response)
echo "TRUE";
else
echo "FALSE";
because if i just do:
echo $response;
But then I get this message:
"Catchable fatal error: Object of class stdClass could not be converted to string".
I tried to strictly define the variable as a Boolean, but that also didn't work. I think i need to do an extra step, i just can not find that extra step on Google or on Stack overflow.
Try:
if ($response->CheckIfUserCridentialsAreValidResult) { ...
Extension from https://stackoverflow.com/a/55191/547210
I am creating a validating function to check several attributes of string variables, which may or may not have been set. (One of the attributes which is checked)
What I am trying to do with the function is receive arguments an unknown number of arguments in the form (See below), and suppress errors that may be caused by passing an unset variable.
I'm receiving the variables like validate([ mixed $... ] ) by using func_get_args()
The previous post mentioned that it was possible by passing by reference, now is this possible when the variables are passed implicitly like this?
If you pass a variable that is not set in the calling scope, the array returned by func_get_args() will contain a NULL value at the position where the variable was passed, and an error will be triggered. This error is not triggered in the function code itself, but in the function call. There is, therefore, nothing that can be done to suppress this error from within the code of the function.
Consider this:
function accepts_some_args () {
$args = func_get_args();
var_dump($args);
}
$myVar = 'value';
accepts_some_args($notSet, $myVar);
/*
Ouput:
Notice: Undefined variable: notSet in...
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/
As you can see, the variable name notSet appears in the error, telling us that the error was triggered in the caller's scope, not that of the callee.
If we want to counter the error, we could do this:
accepts_some_args(#$notSet, $myVar);
...and prefix the variable names with the evil # operator, but a better solution would be to structure our code differently, so we can do the checks ourselves:
function accepts_some_args ($args) {
var_dump($args);
}
$myVar = 'value';
$toPassToFunction = array();
$toPassToFunction[] = (isset($notSet)) ? $notSet : NULL;
$toPassToFunction[] = (isset($myVar)) ? $myVar : NULL;
accepts_some_args($toPassToFunction);
/*
Ouput:
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/
I am using some XML parser to get some information from API, blah blah... :)
In one place in my script, I need to convert string to int but I'm not sure how...
Here is my object:
object(parserXMLElement)#45 (4) {
["name:private"]=>
string(7) "balance"
["data:private"]=>
object(SimpleXMLElement)#46 (1) {
[0]=>
string(12) "11426.46"
}
["children:private"]=>
NULL
["rows:private"]=>
NULL
}
I need to have this string "11426.46" stored in some var as integer.
When I echo $parsed->result->balance I get that string, but if I want to cast it as int, the result is: 1.
Please help!
Thanks a lot!
you have an object, intval of an object will always be 1(if it doesnt have a __toString() magic method defined).
you can intval SimpleXMLElement and it will return 11426, but to do that, the data member of the parserXMLElement class has to be public. you might need to define a getData() method for the parserXMLElement class or make the data member public.
You need to use intval. For example:
echo intval($parsed->result->balance);
will output the value as an integer - assuming that balance is a string.