__toString() with strval() and settype()

php.internals

Adam Maccabee Trachtenberg

22 years ago
Question: Given an object, $obj, should strval($obj) and settype($obj, 'string') return $obj->__toString() if it exists? They currently do not, but (string) $obj does. Are these three operations supposed to be identical, but with different syntax? Or are there other differences among them? -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Marcus Börger

22 years ago
Hello Adam, Thursday, January 15, 2004, 7:04:09 PM, you wrote:
> Question:
> Given an object, $obj, should strval($obj) and settype($obj, 'string') > return $obj->__toString() if it exists?
> They currently do not, but (string) $obj does.
> Are these three operations supposed to be identical, but with > different syntax? Or are there other differences among them?
Looking at the docs i found this: Converting to string You can convert a value to a string using the (string) cast, or the strval() function. String conversion is automatically done in the scope of an expression for you where a string is needed. This happens when you use the echo() or print() functions, or when you compare a variable value to a string. Reading the manual sections on Types and Type Juggling will make the following clearer. See also settype(). That means strval($x) is equal to (string)$x. Looking at settype() i found this: bool settype ( mixed var, string type) Set the type of variable var to type. [...] See also gettype(), type-casting and type-juggling. That does not sound like typecasting but it dosn't sound like no typecasting being involved too. In other words it is unclear. Looking at TypeJuggling i found: If you wish to force a variable to be evaluated as a certain type, see the section on Type casting. If you wish to change the type of a variable, see settype(). This sounds more like no typecasting but like a string representation. Which could be result of __toString() as well as a text like 'object of type ...' or somthing similar. So i'd go for strval() calling __tostring() and hearing some other meanings on settype().
-- Best regards, Marcus mailto:helly@php.net

Adam Maccabee Trachtenberg

22 years ago
On Thu, 15 Jan 2004, Marcus Boerger wrote: Marcus --
> Thursday, January 15, 2004, 7:04:09 PM, you wrote: >
> > Given an object, $obj, should strval($obj) and settype($obj, 'string') > > return $obj->__toString() if it exists? > > > They currently do not, but (string) $obj does. > > > Are these three operations supposed to be identical, but with > > different syntax? Or are there other differences among them? > > Looking at the docs i found this: > Converting to string > You can convert a value to a string using the (string) cast, or the > strval() function. String conversion is automatically done in the scope > of an expression for you where a string is needed. This happens when > you use the echo() or print() functions, or when you compare a variable > value to a string. Reading the manual sections on Types and Type Juggling > will make the following clearer. See also settype(). > > That means strval($x) is equal to (string)$x.
That's how I believe it. I see (string) as the way people who know C cast variables in PHP and strval() as the way non-C programmers cast stuff. :) Later on down on that page, it stays that you can't take a strval() of an object, but I think that's only because before __toString() there was no meaningful way to handle this.
> Looking at settype() i found this: > bool settype ( mixed var, string type) > Set the type of variable var to type. > [...] > See also gettype(), type-casting and type-juggling. > > That does not sound like typecasting but it dosn't sound like no > typecasting being involved too. In other words it is unclear.
I had always considered settype() to be a generic version of strval(), intval(), etc. But maybe it's not.
> So i'd go for strval() calling __tostring() and hearing some other meanings > on settype().
I would like it if strval() called __toString() and I think it also makes sense for settype() to work this way, but maybe it would be good to hear from others, in particular maybe the someone who added these functions? -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Adam Maccabee Trachtenberg

22 years ago
Anyone else care to chime in here? If we're really moving to RC1, I think it makes sense to lock down as many language-level features as soon as possible. -adam On Thu, 15 Jan 2004, Adam Maccabee Trachtenberg wrote:
> On Thu, 15 Jan 2004, Marcus Boerger wrote: > > Marcus -- > > > Thursday, January 15, 2004, 7:04:09 PM, you wrote: > > > > > > Given an object, $obj, should strval($obj) and settype($obj, 'string') > > > return $obj->__toString() if it exists? > > > > > They currently do not, but (string) $obj does. > > > > > Are these three operations supposed to be identical, but with > > > different syntax? Or are there other differences among them? > > > > Looking at the docs i found this: > > Converting to string > > You can convert a value to a string using the (string) cast, or the > > strval() function. String conversion is automatically done in the scope > > of an expression for you where a string is needed. This happens when > > you use the echo() or print() functions, or when you compare a variable > > value to a string. Reading the manual sections on Types and Type Juggling > > will make the following clearer. See also settype(). > > > > That means strval($x) is equal to (string)$x. > > That's how I believe it. I see (string) as the way people who know C > cast variables in PHP and strval() as the way non-C programmers cast > stuff. :) > > Later on down on that page, it stays that you can't take a strval() of > an object, but I think that's only because before __toString() there > was no meaningful way to handle this. > > > Looking at settype() i found this: > > bool settype ( mixed var, string type) > > Set the type of variable var to type. > > [...] > > See also gettype(), type-casting and type-juggling. > > > > That does not sound like typecasting but it dosn't sound like no > > typecasting being involved too. In other words it is unclear. > > I had always considered settype() to be a generic version of strval(), > intval(), etc. But maybe it's not. > > > So i'd go for strval() calling __tostring() and hearing some other meanings > > on settype(). > > I would like it if strval() called __toString() and I think it also > makes sense for settype() to work this way, but maybe it would be good > to hear from others, in particular maybe the someone who added these > functions? > > -adam > >
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Christian Schneider

22 years ago
Adam Maccabee Trachtenberg wrote:
> Anyone else care to chime in here? If we're really moving to RC1, I
So to summarize that'd be echo, print, (string), strval() and settype() using __toString(), right? Sounds +1 to me. Right now strval() and settype() don't call __toString() but exit() does. - Chris

Sterling Hughes

22 years ago
> Adam Maccabee Trachtenberg wrote: > >Anyone else care to chime in here? If we're really moving to RC1, I > > So to summarize that'd be > echo, print, (string), strval() and settype() > using __toString(), right? Sounds +1 to me. >
settype() is a tricky one - i think it should fail for objects, as it could cause destruction of the underlying object, as we don't have magic (in the perl internals meaning of the world). -sterling
-- "Reductionists like to take things apart. The rest of us are just trying to get it together." - Larry Wall, Programming Perl, 3rd Edition

Adam Maccabee Trachtenberg

22 years ago
On Fri, 16 Jan 2004, Sterling Hughes wrote:
> > Adam Maccabee Trachtenberg wrote: > > >Anyone else care to chime in here? If we're really moving to RC1, I > > > > So to summarize that'd be > > echo, print, (string), strval() and settype() > > using __toString(), right? Sounds +1 to me. > > > > settype() is a tricky one - i think it should fail for objects, as it > could cause destruction of the underlying object, as we don't have > magic (in the perl internals meaning of the world).
Ah right. settype() modifies the original object instead of returning a cast version of the object. Yea, settype() should probably fail for objects. So, it's: echo, print, (string), strval(), "$obj", and printf("%s", $obj) (plus printf() variants). That's my complete list. AFAIK, everything but strval() works right now. We just need to fix that function. Also, I don't know why exit() would call __toString(). That doesn't make sense to me. -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!