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 "<br><br><br><br>\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;F0O>F5N9"YC#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!O<VET;W)Y+UIE;F0O>F5N9"YC+'8-<F5T<FEE=FEN9R!R
M979I<VEO;B Q+C$S-@UD:69F("UU("UR,2XQ,S8@>F5N9"YC#2TM+2!:96YD
M+WIE;F0N8PDR,# Q+S X+S$Q(#$U.C4W.C W"3$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)97AP<E]C;W!Y+3YV86QU92YS='(N;&5N(#T@<VEZ96]F*")!<G)A
M>2(I+3$[#2 )"0EE>'!R7V-O<'DM/G9A;'5E+G-T<BYV86P@/2!E<W1R;F1U
M<"@B07)R87DB+"!E>'!R7V-O<'DM/G9A;'5E+G-T<BYL96XI.PT@"0D)8G)E
M86L[#2LC:68@, T@"0EC87-E($E37T]"2D5#5#H-( D)"65X<')?8V]P>2T^
M=F%L=64N<W1R+FQE;B ]('-I>F5O9B@B3V)J96-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
M;" J<W1R=F%L.PTK"0D@(" @>G9A;" J9G5N8U]N86UE.PTK(" @(" @(" @
M(" @04Q,3T-?6E9!3"AF=6YC7VYA;64I.PTK(" @(" @(" @(" @04Q,3T-?
M6E9!3"AS=')V86PI.PTK"0D@(" @6E9!3%]35%))3D<H9G5N8U]N86UE+" B
M7U]T;U-T<FEN9R(L(#$I.PTK(" @(" @(" @(" @:68H8V%L;%]U<V5R7V9U
M;F-T:6]N7V5X*$Y53$PL("9O<"P@9G5N8U]N86UE+" F<W1R=F%L+" P+"!.
M54Q,+" P+"!.54Q,(%134DU,4U]#0RD@(3T@4U5#0T534R!\?"!S=')V86PM
M/G1Y<&4@/3T@25-?3T)*14-4*0TK(" @(" @(" @(" @>PTK"0D)(" @('IV
M86Q?9'1O<BAO<"D[#2L)"0D@(" @;W M/G9A;'5E+G-T<BYV86P@/2!E<W1R
M;F1U<%]R96PH(D]B:F5C="(L('-I>F5O9B@B3V)J96-T(BDM,2D[#2L)"0D@
M(" @;W M/G9A;'5E+G-T<BYL96X@/2!S:7IE;V8H(D]B:F5C="(I+3$[#2L)
M"0D@(" @>F5N9%]E<G)O<BA%7TY/5$E#12P@(D]B:F5C="!T;R!S=')I;F<@
M8V]N=F5R<VEO;B(I.PTK(" @(" @(" @(" @?2!E;'-E('L-*PD)"2 @("!Z
M=F%L7V1T;W(H;W I.PTK"0D)(" @("IO<" ]("IS=')V86P[#2L)"0D@(" @
M>G9A;%]C;W!Y7V-T;W(H;W I.PTK"0D)(" @(&-O;G9E<G1?=&]?<W1R:6YG
M*&]P*3L-*R @(" @(" @(" @(" @(" O+WIV86Q?9'1O<BAS=')V86PI.PTK
M(" @(" @(" @(" @?0TK"0D)>G9A;%]D=&]R*&9U;F-?;F%M92D[#2 )"0EB
M<F5A:SL-*PD@(" @?0T@"0ED969A=6QT.@T@"0D)>G9A;%]D=&]R*&]P*3L-
6( D)"5I604Q?0D]/3"AO<"P@,"D[#0``
`
end


-- 
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]

Reply via email to