Re: [RFC] Scalar Type Hinting With Casts (re-opening)
| From: | Rowan Collins | Date: | Sun, 13 Jul 2014 16:29:51 +0000 |
| Subject: | Re: [RFC] Scalar Type Hinting With Casts (re-opening) | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75428@lists.php.net to get a copy of this message | ||
On 13/07/2014 16:56, Andrea Faulds wrote:
Somewhat off-topic, but maybe we should make StdClass be the actual base class? I mean, it’s already used as a generic object, and you could then put StdClass as a type hint if you’re using an object as a key/value store but want to allow sophisticated objects to be passed. Also, we don’t have a mechanism to cast between classes mostly because we don’t need it; if Bar extends Foo and a function takes a Foo, it can also take a Bar.It's an interesting thought, but we currently have the object keyword for that particular use case. In some languages, having a base object is useful because functionality can be defined there, but PHP provides no way of altering existing classes, limiting that use too. Personally, I'm too much of a fan of PHP's arrays to bother with stdClass at all; if you want to future-proof things, it's not that hard to write placeholders ready for later implementation: class MyValueObject {
/** @todo Declare public properties */} or: interface UsefulObject {
/** @todo Not sure what to enforce yet */} Allowing an array to specific-class cast would be a useful short-hand initialiser in that case (although you'd then want an __castFromArray override when you did flesh out the class): $foo = (MyValueObject)['id' => 42, 'name' => 'The Answer']; -- Rowan Collins [IMSoP]