Page 1 of 1

Cast to custom type

Posted: Thu Nov 06, 2008 5:10 am
by tobym
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

Re: Cast to custom type

Posted: Thu Nov 06, 2008 8:36 am
by tobym
PS Is this the right place to ask?!

Re: Cast to custom type

Posted: Thu Nov 06, 2008 8:41 am
by crazycoders
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:

Code: Select all

 
class mycustomclass{
public static function fromSimpleXMLElement(SimpleXMLElement $obj){
$o = new mycustomclass();
//import $obj into $o
return $o;
}
}
$NewObj = mycustomclass::fromSimpleXMLElement($element);
 
Have fun

Re: Cast to custom type

Posted: Thu Nov 06, 2008 9:07 am
by tobym
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

Re: Cast to custom type

Posted: Thu Nov 06, 2008 9:58 am
by pickle
Actually there is a casting mechanism:

Code: Select all

<?PHP
 
echo (int)1.25);
 
?>
Will display '1'

There is no way to have a custom type however.

Re: Cast to custom type

Posted: Thu Nov 06, 2008 10:10 am
by tobym
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

Re: Cast to custom type

Posted: Thu Nov 06, 2008 10:47 am
by RobertGonzalez
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&#211;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>