In PHP, I'm requesting data from my QuickBooks Online account using their Query functions.
I'm iterating through each customer object that is returned in a standard foreach($customer as $x) code block.
Using var_dump, I can tell each object has the following format:
object(QuickBooksOnline\API\Data\IPPCustomer) [34]
public 'Taxable' => string 'true' (length=4)
public 'BillAddr' =>
object(QuickBooksOnline\API\Data\IPPCustomer) [78]
public 'Id' => string '7509' (length=4)
public 'Line1' => '1234 Irrelevant Dr.' (length=19)
In PHP, if I simply try to access $x->Taxable, it works no problem, but any sub-object I try to access returns an error: "trying to get property of non-object on line X"
So:
echo $x->Taxable;
gives me the taxable status, but
echo $x->BillAddr->Line1;
gives me an error.
These are public classes, so shouldn't I be able to call them like this?
Found the problem and I have no idea why this is required, but here's how I got it to work, adding curly braces:
echo "{$x->BillAddr->Line1}"; instead of just echo "$x->BillAddr->Line1";
Same rule applies to assignment as well, apparently:
$line1 = "{$x->BillAddr->Line1}";
Thanks for your help, guys!
Related
I have this object :
var_dump ($filtres) ;
Result:
object(stdClass)[2]
public 'filtres' =>
object(stdClass)[3]
public 'table' => string 'crm_comptes' (length=11)
I'm simply trying to echo it, it doesn't work :
echo $filtres->table;
It stays BLANK, I've tried plenty of things, like converting it to an array, all of that, it still doesn't want to display 'crm_comptes'
I've tried this :
echo $filtres['table'];
It still displays nothing.
Did I do something wrong?
As I have seen you output result, it seems your result $filtres is an object of an object for table. You should get it as:
$filtres = $filtres->filtres;
echo $filtres->table;
A library I use uses an array. Applying print_r to that array prints this:
Array
(
[*queueId] => 1
[*handle] => 9b875867b36d568483fb35fdb8b0bbf6
[*body] => First string in the TestQueue
[*md5] => c23ba714199666efbc1dcd5659bb0a0a
[*timeout] => 1408003330.6534
[*id] => 2
[*creationdate] => 2014-08-13 16:03:37
)
The library uses a magic getter on that array
public function __get($key)
{
if (!array_key_exists($key, $this->_data)) {
throw new Exception\InvalidArgumentException("Specified field \"$key\" is not in the message");
}
return $this->_data[$key];
}
When I try to access
$myObject->body
I run into the exception. In fact, the debugger shows that array_key_exists will return false while the _data array is available as printed above
The asterisk indicates that this array is a representation of an object, probably the original object property is protected.
http://php.net/manual/en/language.types.array.php#language.types.array.casting
As I explained in the comments, the array keys actually start with an asterisk. Since you can't call them using the regular syntax of $obj->*body (it'll cause a syntax error), you can use the following:
$myObject->{'*body'}
This should solve your problem.
Assuming that $myObject is the array you are talking from:
You can't access arrays with ->, use $myObject['*body'] instead. (And you should as well change the name to $myArray, for example)
As #MarkBaker stated in the comment of my question, the problem was that I was serializing an object with private properties to the array. The asterisk were marks that these properties were private.
Hi all i have a function in a class which will print all the variable pass for this function
Class Code :
<?php
class MyPrintClass {
public function printVaraible() {
var_dump(func_get_args())
}
}
$printVar = new MyPrintClass;
?>
if i use like below its correctly working and printing all the values pass.
$print->printVaraible('value1','Value2',20);
The output i got for above cmd
array (size=1)
0 => string 'value1' (length=6)
1 => string 'value2' (length=6)
2 => string 'value3' (length=6)
if i use like blow its showing as a single array.. i want the value seperated.. how its possible ?
function getVal(){
global $printVar;
$print->printVaraible(func_get_args());
}
I need to pass the value like below
getVal('value1','Value2',20);
and i need the output as
array (size=1)
0 => string 'value1' (length=6)
1 => string 'value2' (length=6)
2 => string 'value3' (length=6)
Currently i get NULL as the output
Updated Question Based On the Answer Given By deceze
** I Also had a small change in my code**
Class Code :
<?php
class MyPrintClass {
public function printVaraible($tag,$value) {
echo $tag.' == '.$value;
var_dump(func_get_args());
}
}
$printVar = new MyPrintClass;
?>
Converted Class To Function By
<?php
function getVal($tag,$value) {
global $printVar;
call_user_func_array([$printVar, 'printVariable'], $tag,$value,func_get_args());
}
?>
If i try to use it as below i get error
<?php getVal('first','second','third,'fourth'); ?>
Warning: call_user_func_array() expects exactly 2 parameters, 4 given
call_user_func_array([$print, 'printVariable'], func_get_args());
This calls the function with separate parameters from an array. I'd question the usefulness of this though, since you'll just end up with an array from func_get_args anyway. Why not pass arrays around in the first place?
Your getting null in the return because you never return anything, from the functions above.
if you want to call a class method I suggest you look into Refection or you can also do
$a = 'printVaraible';
$class->$a(1,3); //
besides using call_user_func_array, I would also suggest looking into closures (php > 5.3 ), which will let you write them this way.
$a = function foo(){ echo 'foo'};
$a();
Last thing I would generally avoid using global, as it obscures in your code where the values come from. It is always better to inject values into the function scope and not mix that with the global, imaging hunting that global down in a large project, I just avoid them at all cost and forget they exist.
I know there is alot of information available in regards to PHP - var_dump my question is if I need to pick the particular information from the dump I am receiving from the API which is returning different array object....for example the below dump I need the information of realm under characterData Array how can I pick the that information and stored in String variable. Brief example of php code to explain my question will be highly appreciated...
object(Character)[3]
private 'name' => string 'XXXX' (length=6)
private 'region' => string 'eu' (length=2)
private 'realm' => string 'Defias Brotherhood' (length=18)
private 'characterData' =>
array (size=24)
'lastModified' => float 1363345999000
'name' => string 'Growar' (length=6)
'realm' => string 'Defias Brotherhood' (length=18)
'battlegroup' => string 'Rampage / Saccage' (length=17)
basically this information is coming from battlegroup api
$armory = new BattlenetArmory('EU','Defias Brotherhood');
$armory->setLocale('ru_RU');
// To reset back to default server locale
$armory->setLocale(FALSE);
//initialize the character to get the character object
$character = $armory->getCharacter('XXXX');
If you don't have a getter for the field characterData, you won't have access to it because it is a private property, unless you use Reflection:
$ref = new ReflectionClass( 'Character');
$prop = $ref->getProperty( 'characterData');
$prop->setAccessible( true);
$array = $prop->getValue( $yourCharacterObject);
echo $array['realm'];
This can be tested with a simple class that mimics the OPs:
class Character {
private $characterData;
public function __construct() {
$this->characterData['realm'] = 'Defias Brotherhood';
}
}
Currently, others have the proposed this solution:
$yourCharacterObject = new Character();
// echo $yourCharacterObject->characterData['realm'];
Which results in:
Fatal error: Cannot access private property Character::$characterData in X on line 11
However, Reflection will be able to grab the private value and display it without error, as shown in this demo, which uses the above class and code to print the desired property value.
Character is a class that you create instance from - for example:
$c = new Character();
since characterData is private you can't access it via $c->characterData
You need to use some public access function (like getCharacterData) or if you have access inside the object just changed it according to your needs
Given the following code
$c= new SoapClient('http://www.webservicex.net/CurrencyConvertor.asmx?WSDL');
$usa = "USD";
$eng = "GBP";
doing a __getTypes on the client gives me
Array ( [0] => struct ConversionRate { Currency FromCurrency; Currency ToCurrency; } [1] => string Currency [2] => struct ConversionRateResponse { double ConversionRateResult; } )
if i then do
$calculation = $c->ConversionRate($usa, $eng);
and print calculation i get an error about
Catchable fatal error: Object of class stdClass could not be converted to string
Is there a specific way i should be printing this out, or i it a bug, from researching / googling many people seem to have a problem but i cant find a suitbale solution, other than downgrading php, which isnt a solution for me as i am doing this as homework and its running off of a college server
I'm guessing the return type from that function is not a string (or anything with __toString defined). Normally instances of stdClass will have one or more properties which will be of use to you.
Try doing something like:
print_r($calculation)
That should tell you what the object has on it, and what it is you might want to do with it. I'd guess you'd want to be printing some property off there along the lines of (example):
echo $calculation->result;
Try passing the parameters as an array:
$parameters = array('FromCurrency' => "USD",
'ToCurrency' => "GBP");
$calculation = $soapClient->ConversionRate($parameters)
var_dump($calculation);
var_dump() could highlight that your result is an object and the double could be a member of that object.
Example:
$calculation->ConversionRateResult;