Re: Class & Type Casting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 10 June 2008, Leurent Francois wrote:
> I was asking myself if there was a good reason this syntax not to work
>
> class string_extented{
> function __construct($str){
> $this->contents=$str;
> }
> function __toString(){
> return $this->contents;
> }
>
> }
>
> //that's working fine
> $test= new string_extended("this is my anonymous string");
> echo $test;
>
>
> /that's not working, but should'nt it be the case ?
> $test = (string_extended) "This is my anonymous string";
> echo $test;
>
> I'll find this very usefull :x, it's just a syntax ehancement nope ?

Casting only works between "easily convertable" types.  A string does not 
easily convert to a classic object.  One could ask if it would just pass to 
the constructor, but it doesn't.  I don't know if there's a technical reason 
for that or if just no one implemented it, but the short answer is "The 
language doesn't do that."

(What may be confusing you is __toString(), which only works for *reading* if 
you *already have an object* and then use it in a string context.  You don't 
already have an object and are trying to *write*, so the __toString() does 
nothing.)

-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux