Re: __toString() with strval() and settype()
| From: | Marcus Boerger | Date: | Thu, 15 Jan 2004 19:48:33 +0000 |
| Subject: | Re: __toString() with strval() and settype() | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-7131@lists.php.net to get a copy of this message | ||
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