Re: __toString & friends

From: Date: Fri, 19 Sep 2008 11:58:46 +0000
Subject: Re: __toString & friends
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-280494@lists.php.net to get a copy of this message
Is there any feature submission process were we could discuss of this subject ? "Colin Guthrie" <gmane@colin.guthr.ie> a écrit dans le message de news: gapele$188$1@ger.gmane.org... > 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 (#280494) next »