Re: [RFC] Scalar Type Hinting With Casts (re-opening)

From: Date: Sun, 13 Jul 2014 16:36:24 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5 6 7 8 9 10 11 12  Groups: php.internals 
Request: Send a blank email to internals+get-75430@lists.php.net to get a copy of this message
On 13 Jul 2014, at 17:29, Rowan Collins <rowan.collins@gmail.com> wrote: > 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. There’s no such type hint though: $ php -r 'function foo(object $x) {} foo(new StdClass);’ PHP Catchable fatal error: Argument 1 passed to foo() must be an instance of object, instance of stdClass given, called in Command line code on line 1 and defined in Command line code on line 1 Perhaps one should be added? Not in this RFC though, obviously. > 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. I’m thinking more about dealing with JSON and such which give you an object. Perhaps you might have a FooBar->fromObject() method that can be passed another FooBar to clone it, or an object decoded from JSON. Though in that particular case there’s no real need for a type hint. -- Andrea Faulds http://ajf.me/

« previous php.internals (#75430) next »