Cast to custom type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tobym
Forum Newbie
Posts: 4
Joined: Thu Nov 06, 2008 5:06 am

Cast to custom type

Post 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
tobym
Forum Newbie
Posts: 4
Joined: Thu Nov 06, 2008 5:06 am

Re: Cast to custom type

Post by tobym »

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

Post 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
tobym
Forum Newbie
Posts: 4
Joined: Thu Nov 06, 2008 5:06 am

Re: Cast to custom type

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Cast to custom type

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tobym
Forum Newbie
Posts: 4
Joined: Thu Nov 06, 2008 5:06 am

Re: Cast to custom type

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Cast to custom type

Post 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>
Post Reply