Re: __toString & friends

From: Date: Tue, 16 Sep 2008 23:14:54 +0000
Subject: Re: __toString & friends
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-280325@lists.php.net to get a copy of this message
Nathan Rixham wrote:
concurred; I recently made a class that turn's all objects into XML; and implented it in my __toString()'s :)
In that case rather than __toBool() __toInt() etc. it should really support a magic __cast() method or similar. This method would contain a single argument that represents the type (e.g. a class name or a builtin type.... function __cast($type) { switch ($type) {
    case 'string':
      return 'Foo';
    case 'array':
      return array('Foo');
    case 'DomDocument':
      // etc.
} } And if you did: $foo = new MyClass; $xml = (DomDocument)$foo; It would return the result of __cast called with $type == 'DomDocument'. That would be nice and generic. I could see that being quite useful (tho' arguably, itmplementing a cast() method is not that hard: $xml = $foo->cast('DomDocument'); It's not as neat tho' :) col -- Colin Guthrie gmane(at)colin.guthr.ie http://colin.guthr.ie/ Day Job: Tribalogic Limited [http://www.tribalogic.net/] Open Source: Mandriva Linux Contributor [http://www.mandriva.com/] PulseAudio Hacker [http://www.pulseaudio.org/] Trac Hacker [http://trac.edgewall.org/]

« previous php.general (#280325) next »