I am trying to do a simple multiplication in an object-oriented PHP project for an XML file.
In the MariaDB, the data is saved as float(8.2). I get this error:
The object of class ArticlePrice could not be converted to int in [file online...]
$item->addChild('price', $article->getPrice(CountryPublic::getByShortCut('a')));
$item->addChild('newPrice', $productPriceNew*1.8);
Then I tried to convert it to float (instead of minifloat) like this:
$productPrice = $article->getUsualPrice(CountryPublic::getByShortCut('a'));
$productPriceNew = (float)$productPrice*1.8;
The message I get is this:
The object of class ArticlePrice could not be converted to double in
What am I doing wrong?
you actually try to cast an object into a float.
$article->getUsualPrice(CountryPublic::getByShortCut('a'));
Your method getUsualPrice return an object ArticlePrice, you should put a getter on your class ArticlePrice to access your attribut price inside it and then cast it into a float.
It's the straightforward solution but probably not the most beautiful in term of conception.
if you try something like
var_dump($article->getUsualPrice(CountryPublic::getByShortCut('a')) instanceof ArticlePrice)
You see that you cast an object in float.
After your commentary
object(ArticlePrice)#11396 (15) {
["id":protected]=> string(36) "7f01d63a-3f08-480a-b798-c83f6ddbdb94"
["articleID":protected]=> string(36) "65983c99-66e4-43689ba7039dc5e742c0"
["countryID":protected]=> string(36) "31149178-8a2a-4e57-8133-ca12004a59dd"
["price":protected]=> string(5) "13.50"
you see that you got on attribut called price.
you only need to do your operation on that attribut not on your object.
you probably have a getter on your class something called getPrice().
The following code will working:
(float)$article->getPrice(CountryPublic::getByShortCut('a'))->getPrice()
but you probably need to take a moment to think about the name of your method, it is redundant to have two methods called getPrice()
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to convert object into string in php
I have a variable that contain some object (SimpleXML).
Can I change the type of this variable, and to assing it to this variable itself?
Like this:
$test = (string)$test;
var_dump($test);
The above code does not work, so the output is still object(SimpleXMLElement) and not a string.
But when I assign it another variable, like $new_test = (string)$test it works well, and the var_dump output is string]
If you want the string content, use asXML method.
var_dump($test->asXML());
It depends on how SimpleXML implements the magic function __toString(). Its different from class to class. But if its not implemented, PHP will throw a fatal error.
So, typecasting directly from object to string does not work unless the __toString() method is implemented.
You can't convert an object to string just like adding a declaration, it might work but it wont behave like desired there's a greate article though written before here in stack on how you should do it the optimal way which is by adding a tostring method read more here...
how to convert object into string in php
It depends on the object being converted. For SimpleXML you probably want its asXML method: http://www.php.net/manual/en/simplexmlelement.asxml.php. For general objects, you can typecast to string if the objects implements the __toString() method. Another option would be var_export(...,true), but that is rarely useful except for debugging.
Typecast the SimpleXMLObject to a string
$foo = array( (string) $xml->parent->child );
<?php
$xmlstring = "<parent><child> hello world </child></parent>";
$xml = simplexml_load_string($xmlstring);
$foo = array( (string) $xml->child );
var_dump($xml).PHP_EOL;
var_dump($foo);
?>
Output
object(SimpleXMLElement)#1 (1) {
["child"]=>
string(13) " hello world "
}
array(1) {
[0]=>
string(13) " hello world "
}
http://codepad.org/Bss1rndd
I am trying to perform count on SimpleXML Element. It si giving me different results on PHP 5.3 and PHP 5.2. My code looks like follows :
$xml = new SimpleXMLElement('<command action="foo"/>');
print_r(count((array)$xml->children()));`
On PHP 5.2 the above prints "1" and on PHP 5.3 it prints "0" :(
I know I can use $xml->count but that does not take ino account the root element of the XML.
Just wondering what might be wrong in type casting the SimpleXML to array in PHP 5.3
Well, SimpleXMLElement::children() will always return a SimpleXMLElement instance according to the manual.
And it does according to var_dump.
But as command has no child, the returned object has no accessible attributes which yields to 0 when casting to array:
If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible; private variables have the class name prepended to the
variable name; protected variables have a '*' prepended to the
variable name. These prepended values have null bytes on either side.
Couldn't find anything about changed type casting to array for 5.2 to 5.3.
So this might (might) be a bug ...
I'm using SimpleXML to get some data from an API. Its returning things in this format:
object(SimpleXMLElement)#10 (1) {
[0]=>
string(36) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
My question is, how can I possibly access the string value of this object? If I try to do $myVariable->0 that gives me an error. Doing $zero = '0' and then echo $myVariable->$zero doesn't work either, nor does (array) $myVariable work (that gives a warning).
The trick is that SimpleXMLElement has __toString magic method implemented that would return your string(36) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", so to get this string you just cast (string) on your SimpleXMLElement object:
(string)$myVariable
With PHP you can
print $myVariable;
of course, so explicit (string) here is not necessarily needed.
AFAIR it's like this:
$myVariable->{0}
Edit: That would work in majority of cases, but not this one. It looks like SimpleXML implements not only __toString method like Nemoden pointed out, but also __get, so that accessing object properties in this way results in cloned object being returned.
I am trying to output a set of data, and I have, due to my LEFT OUTER JOIN got multiple rows per match.
I'm using a positive look ahead to see if the next row is the same id as the first. However the return from the database class returns an array of objects.
array(7) {
[0]=> object(stdClass)#4 (8) {
["dom_id"]=>string(1) "3"
["domain"]=>string(11) "example.com"
["status"]=>string(7) "Invalid"
["expiry"]=>string(10) "2010-07-20"
["remaining"]=>string(6) "0 Days"
["rem_id"]=>NULL
["alert_type"]=>NULL
["contact"]=>NULL
}
//etc
I am getting the following error, Fatal error: Cannot use object of type stdClass as array
My code is as follows,
echo $domains[$k+1]->alert_type;
I know that I could assign the new dimension to a variable and access that as an object, but for the sake of neatness I'd rather access it directly.
Is this possible? ..and if it is, how do I approach it?
Ta
Either
fetch as array instead of stdClass or
typecast to (array) or
use a custom class that implements ArrayAccess.
Assuming that $domains is your stdClass object which you wish was an array, you could try using variable variables:
echo $domains->{$k+1}->alert_type;
The real problem though is that you have a bunch of stdClass objects. Are you storing these in a session and then reading them later, or are you neglecting to include a file or something... ?
From what I can tell, a SimpleXMLElement is just an array of other SimpleXMLElements, plus some regular array values if there wasn't a tag nested in another tag.
I have a SimpleXMLElement in a variable $data, and var_dump($data) gives me this:
object(SimpleXMLElement)#1 (33) {
["buyer-accepts-marketing"]=>
string(4) "true"
...
...
but calling var_dump($data->buyer-accepts-marketing) gives me an error, and var_dump($data["buyer-accepts-marketing"]) gives me NULL. Calling var_dump($data->shipping-address->children()) gives me an error.
going like this:
foreach($data as $item) {
var_dump($item);
}
gives a whole bunch of SimpleXMLElement objects, but oddly enough, no strings or ints.
What am I missing here? I want to take specific portions of it and pass them to a function, so for example, I don't have to go
$data->billing-address->postal-code;
...
$data->shipping-address->postal-code;
...
and can just go
address($data->billing-address);
address($data->shipping-address);
etc.
SimpleXMLElement is not just an array. To access child elements, you must use object notation ($a->b) and to access attributes you must use array notation ($a['b']).
Problem is, with object notation, valid tag names can be illegal PHP code.
You need to do this:
$data->{'buyer-accepts-marketing'};
Note that this returns a SimpleXMLElement! The reason for this is that it can contain either just text, more child elements, or both. The output of var_dump() is very misleading for SimpleXMLElements. If you want to the text content of a single <buyer-accepts-marketing> tag, you have to do this:
(string)$data->{'buyer-accepts-marketing'};
Of course it is also perfectly legal to do this:
(int)$data->{'buyer-accepts-marketing'};
The reason this appears to work in some cases (such as echoing a SimpleXMLElement) is that the type conversions are implicit and automatic. You can't echo an object, so PHP automatically converts it to a string.
I have a love/hate relationship with SimpleXML. It makes things very easy only after you understand how complex the actual API is.
Read up on the so-called "basic" examples to get a good handle on it.