Re: What about a magic __toArray() method?
| From: | Marco Pivetta | Date: | Tue, 04 Feb 2020 13:13:02 +0000 |
| Subject: | Re: What about a magic __toArray() method? | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108372@lists.php.net to get a copy of this message | ||
Hey Tom,
On Tue, Feb 4, 2020 at 1:55 PM Tom Gerrits <tom@inventis.be> wrote:
> "(string) $var" doesn't seem a lot less explicit to me than
> "$var->toString()", but if it is coerced automatically when being
> passed to e.g. a function taking a string, that is of course a
> different story.
>
(string) $var is less explicit because (string) is an operation that
applies to a wide set of types, whereas any -> applies only to object
types, and also has a specific name (the method name) on its right.
Restricting that operation to fewer supported types is not feasible at this
point, since (string) null is very much relied upon.
Coercions and implicit casting using the overloaded operators could
> perhaps also be limited to non-strict mode, if desired, which is also
> the case for scalar types currently, IIRC.
>
Is there any newly written code that doesn't use non-strict mode at all?
Programming without declare(strict_types=1) seems anachronistic at this
point.
Greets,
Marco Pivetta
http://twitter.com/Ocramius
http://ocramius.github.com/