Hi there,
I'm fairly new to PHP and have what's probably a fairly basic question. Is it possible to cast an object of one type to that of a custom type?
I want to cast a SimpleXMLElement object to a custom type (that extends SimpleXMLElement). The reason I want to do it this way is that the SimpleXMLElement was returned from simplexml_load_file() (so I can't just use the constructor of my custom type).
Thanks,
Toby
Cast to custom type
Moderator: General Moderators
Re: Cast to custom type
PS Is this the right place to ask?!
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: Cast to custom type
There are no casting mechanism in PHP because types are completely dynamic relative to the core of the engine. The only types that used to exist at first were variant and array. Nowadays, with classes you have new types but casting doesn't exist in itself.
Your custom class must implement casting by itself through a command of your choice such as "fromSimpleXMLElement(SimpleXMLElement $object)". In that function i'd read the element into the new structure and simply drop the old object and keep the reference to the new object. Additionnaly, i'd make that function static and use it like this:
Have fun
Your custom class must implement casting by itself through a command of your choice such as "fromSimpleXMLElement(SimpleXMLElement $object)". In that function i'd read the element into the new structure and simply drop the old object and keep the reference to the new object. Additionnaly, i'd make that function static and use it like this:
Code: Select all
class mycustomclass{
public static function fromSimpleXMLElement(SimpleXMLElement $obj){
$o = new mycustomclass();
//import $obj into $o
return $o;
}
}
$NewObj = mycustomclass::fromSimpleXMLElement($element);
Re: Cast to custom type
Thanks for that - I had wondered if that might be the case, but thought it worth asking in case I'd missed something.
Cheers,
Toby
Cheers,
Toby
Re: Cast to custom type
Actually there is a casting mechanism:
Will display '1'
There is no way to have a custom type however.
Code: Select all
<?PHP
echo (int)1.25);
?>There is no way to have a custom type however.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Cast to custom type
Sorry, yes, I knew about that - seeing this http://uk.php.net/language.types.type-juggling is what gave me hope that there might be something that could be used for casting more complex types.
Should have mentioned that originally, cheers for pointing it out.
Toby
Should have mentioned that originally, cheers for pointing it out.
Toby
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Cast to custom type
Extend the SimpleXMLElement class with your own and call that instead:
Code: Select all
<?php
/**
* Extend the SimpleXMLElement base class
*/
class MySXML extends SimpleXMLElement {}
/**
* Get the contents of the XML file to parse
*/
$file = file_get_contents('php-net-example.xml');
/**
* Let make our object, using our own instead of the base SimpleXMLElement class
*/
$xml = new MySXML($file);
/**
* Prove it, for good measure
*/
print_r($xml);
?>Code: Select all
<?xml version='1.0' standalone='yes'?><movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El ActÓr</actor> </character> </characters> <plot> So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. </plot> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type="thumbs">7</rating> <rating type="stars">5</rating> </movie></movies>