Hello troels,

Friday, July 25, 2008, 5:04:31 PM, you wrote:

> First, apologies if this has been discussed before. I couldn't find
> any evidence to suggest that it has, but it kind of surprises me.

> As strings aren't objects in PHP, __toString is quite a useful
> construct, but it begs the question as to why there aren't
> __to<primitve-type> for each of the primitive types in PHP? Through
> SPL, we have __toArray covered, and I presume that there is no real
> value in a __toInteger or __toFloat, but __toBoolean seems as it could
> be quite useful, since it would allow an object to be evaluated in a
> conditional. Eg. a simple use-case:

> class Validation {
>   protected $errors = array();
>   function fail($error) {
>     $this->errors[] = $error;
>   }
>   function __toBoolean() {
>     return count($this->errors) === 0;
>   }
> }

> I wonder if the reason why this haven't been suggest yet is because of:
> a) Nobody thought about it before
> b) Somebody thought about it, and figured out that it was a bad idea

> While this looks pretty simple, I have a hunch that introducing such
> behaviour could have far-fetching consequences, as it hooks into PHP's
> dynamic typing system. It also have a smell of C++'s ability to
> overload operators and the ensuing shooting of feet. On the other
> hand, it does allow some nifty tricks, and as it's optional,
> presumably people would only use it when it actually makes sense.

> Even if this, for some reason, doesn't fit into core PHP, it might be
> a candidate for SPL? (Even if the syntax would then be slightly
> different)

No, there is SPL_Types in pecl which is a good place for type stuff.

Actually SPL_Types allows objects that behave as booleans.

Other than that we do not want this type of conversion support and it was
discussed before and rejected every single time.

Best regards,
 Marcus


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to