[PHP-DEV] object to string convertion patch
I changed php (current CVS) to add another magic method to Objects, __toString(). Whenever an object needs to be converted to a string, then it will first try to call __toString() and if it can't, then it will just return Object like now. Please look over my patch and see if I did it correctly, and could someone please put it in cvs? If someone will include this in CVS I will followup with __toDouble() and __toLong() and __toBoolean() and __toArray() My goal is to make Objects more useful as regular data types. For example I could introduce a new Class called Date which would act just like a string in all string contexts and act just like a number of seconds from 1970 in numeric contexts. I have one thing that is confusing to me, look at this test script: ?php class foo { var $fooval; function foo() { $this-fooval=foofoo; } } class bar extends foo { function __toString() { return 5; } } class baz extends foo { function __toString() { return $this-fooval; } } echo brbrbrbr\n; $foo=new foo(); $bar=new bar(); $baz=new baz(); var_dump(5); var_dump(strval(5)); echo print_r(\$foo): ; print_r($foo); echo br\n; echo \$foo: , $foo, br\n; echo strval(\$foo): , strval($foo), br\n; echo var_dump(strval(\$foo)): ; var_dump(strval($foo)); echo br\n; echo print_r(strval(\$foo)): ; print_r(strval($foo)); echo br\n; echo print_r(\$bar): ; print_r($bar); echo br\n; echo \$bar: , $bar, br\n; echo strval(\$bar): , strval($bar), br\n; echo var_dump(strval(\$bar)): ; var_dump(strval($bar)); echo br\n; echo print_r(strval(\$bar)): ; print_r(strval($bar)); echo br\n; echo print_r(\$baz): ; print_r($baz); echo br\n; echo \$baz: , $baz, br\n; echo strval(\$baz): , strval($baz), br\n; echo var_dump(strval(\$baz)): ; var_dump(strval($baz)); echo br\n; echo print_r(strval(\$baz)): ; print_r(strval($baz)); echo br\n; ? Everything works as expected except for the var_dump and print_r calls. Here is the output: Notice the the print_r calls are showing the whole object and not just the strval. int(5) string(1) 5 print_r($foo): foo Object ( [fooval] = foofoo ) $foo: Warning: Object to string conversion in c:\inetpub\wwwroot\default.php on line 37 Object strval($foo): Warning: Object to string conversion in c:\inetpub\wwwroot\default.php on line 38 Object var_dump(strval($foo)): Warning: Object to string conversion in c:\inetpub\wwwroot\default.php on line 39 string(6) Object print_r(strval($foo)): Warning: Object to string conversion in c:\inetpub\wwwroot\default.php on line 40 Object print_r($bar): bar Object ( [fooval] = foofoo ) $bar: 5 strval($bar): 5 var_dump(strval($bar)): object(bar)(1) { [fooval]= string(6) foofoo } print_r(strval($bar)): bar Object ( [fooval] = foofoo ) print_r($baz): baz Object ( [fooval] = foofoo ) $baz: foofoo strval($baz): foofoo var_dump(strval($baz)): object(baz)(1) { [fooval]= string(6) foofoo } print_r(strval($baz)): baz Object ( [fooval] = foofoo ) begin 666 foo.txt M26YD97@Z(%IE;F0OF5N9YC#3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T] M/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T]/3T-4D-3 M(9I;4Z(]R97!OVET;W)Y+UIE;F0OF5N9YC+'8-F5TFEE=FEN9R!R M979IVEO;B Q+C$S-@UD:69F(UU(UR,2XQ,S8@F5N9YC#2TM+2!:96YD M+WIE;F0N8PDR,# Q+S X+S$Q(#$U.C4W.C W3$N,3,V#2LK*R!:96YD+WIE M;F0N8PDR,# Q+S X+S(R(#$V.C,Y.C0T#4! (TQ-#L,3 @*S$T-RPQ,B! M0 T@0D)97APE]C;W!Y+3YV86QU92YS='(N;5N(#T@VEZ96]F*)!G)A M2(I+3$[#2 )0EE'!R7V-O'DM/G9A;'5E+G-TBYV86P@/2!EW1R;F1U M@B07)R87DB+!E'!R7V-O'DM/G9A;'5E+G-TBYL96XI.PT@0D)8G)E M86L[#2LC:68@, T@0EC87-E($E37T]2D5#5#H-( D)65X')?8V]P2T^ M=F%L=64NW1R+FQE;B ]('-IF5O9B@B3V)J96-T(BDM,3L-( D)65X')? M8V]P2T^=F%L=64NW1R+G9A; ](5S=')N9'5P*)/8FIE8W0B+!E'!R M7V-O'DM/G9A;'5E+G-TBYL96XI.PT@0D)8G)E86L[#2LC96YD:68-( D) M95F875L=#H-( D)2IE'!R7V-O'D@/2 J97APCL-( D)7IV86Q?8V]P M5]C=]R*5X')?8V]P2D[#4EN95X.B!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R!\?!S=')V86PM M/G1Y4@/3T@25-?3T)*14-4*0TK( @( @( @( @PTK0D)( @('IV
Re: [PHP-DEV] object to string convertion patch
On Wed, 22 Aug 2001, Jeremy Bettis wrote: I changed php (current CVS) to add another magic method to Objects, __toString(). Whenever an object needs to be converted to a string, then it will first try to call __toString() and if it can't, then it will just return Object like now. Good luck trying to get this in. -Andrei * George Orwell was an optimist. * -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] object to string convertion patch
At 19:53 22-08-01, Andrei Zmievski wrote: On Wed, 22 Aug 2001, Jeremy Bettis wrote: I changed php (current CVS) to add another magic method to Objects, __toString(). Whenever an object needs to be converted to a string, then it will first try to call __toString() and if it can't, then it will just return Object like now. Good luck trying to get this in. I implemented this once, and it was even in for several mini versions. But it causes inconsistencies, and it was thus removed (I don't remember what the inconsistencies were, look up the archives...) Zeev -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] object to string convertion patch
My implementation does not have these inconsistencies (I looked in the archives). The only wierd thing is print_r and dump_var. Zeev Suraski [EMAIL PROTECTED] wrote in message 5.1.0.14.2.20010822195524.03aeb4c8@localhost">news:5.1.0.14.2.20010822195524.03aeb4c8@localhost... At 19:53 22-08-01, Andrei Zmievski wrote: On Wed, 22 Aug 2001, Jeremy Bettis wrote: I changed php (current CVS) to add another magic method to Objects, __toString(). Whenever an object needs to be converted to a string, then it will first try to call __toString() and if it can't, then it will just return Object like now. Good luck trying to get this in. I implemented this once, and it was even in for several mini versions. But it causes inconsistencies, and it was thus removed (I don't remember what the inconsistencies were, look up the archives...) Zeev -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] object to string convertion patch
At 20:08 22-08-01, Jeremy Bettis wrote: My implementation does not have these inconsistencies (I looked in the archives). The only wierd thing is print_r and dump_var. I think the inconsistency that was there was that my implementation was for displaying purposes only. If you call to_string() in convert_to_string(), then it may be consistent (I'm not sure, I haven't thought about it thoroughly). There was a reason on why we didn't switch to this as well, though. Zeev -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DEV] object to string convertion patch
http://www.geocrawler.com/mail/thread.php3?subject=%5BPHP-DEV%5D+__string_value%28%29++crashes+with+returnlist=5 First of all I suggest everyone catches up on the reading :) Thies and I were against it. Zeev wanted to keep it and most of the rest that replied seemed to be against too. Personally I don't have a problem with _to_string() *if* it works well and as expected. There are two main issues: a) Make all of PHP work this way consistently. b) Make the patch itself work well. (a) shouldn't be much of a problem. I think as most string conversions use convert_to_string() there probably aren't too many exceptions. The main problem is (b). What if _to_string() returns a different object which is for example an error object. We would all like this error object to be in turn converted to a string. However, if we make the code general then we could risk having an infinite loop. What Jeremy's patch does to solve this is only call the _to_string() method once and if it returns an object then the result is Object like it is today. I guess it's OK but it would be nicer to have it work recursively. But then again that could lead to infinite loops. (It might be something we can live with). Anyway, so on a whole I don't really mind having such functionality but let's just be sure what kind of behavior we want and if we can make it behave consistently across PHP. Andi At 11:53 AM 8/22/2001 -0500, Andrei Zmievski wrote: On Wed, 22 Aug 2001, Jeremy Bettis wrote: I changed php (current CVS) to add another magic method to Objects, __toString(). Whenever an object needs to be converted to a string, then it will first try to call __toString() and if it can't, then it will just return Object like now. Good luck trying to get this in. -Andrei * George Orwell was an optimist. * -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]