Object[] type hint

php.internals

Sebastian Bergmann

19 years ago
Hi, any chance for the following type hint syntax class Example { public function doSomething(Object[] $objects) { } } instead of having to write class Example { public function doSomething(Array $objects) { foreach ($objects as $object) { if (!$object instanceof Object) { throw new InvalidArgumentException; } } } } Thanks, Sebastian
-- Sebastian Bergmann http://www.sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Stanislav Malyshev

19 years ago
> class Example > { > public function doSomething(Object[] $objects)
And how this would be verified - each time by passing whole array and checking each element is an object? I don't think it's a good idea. Also, PHP doesn't have [] as array definition (at least not until Andi's idea is accepted), only as array resolution, so this syntax is also illogical, moreover, it gives you and idea that you could define types using [], which you can not.
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/

Sebastian Bergmann

19 years ago
Stanislav Malyshev wrote:
> And how this would be verified - each time by passing whole array and > checking each element is an object? I don't think it's a good idea.
The check would only be performed in the case where one uses the new type hint. And if someone needs this, he would manually do the same in userspace anyways (see the example in my original mail).
-- Sebastian Bergmann http://sebastian-bergmann.de/ GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

Stanislav Malyshev

19 years ago
> The check would only be performed in the case where one uses the new > type hint. And if someone needs this, he would manually do the same in > userspace anyways (see the example in my original mail).
I would say (IMHO of course) this belongs to userspace. No reason to build such a complex and rarely used functionality into the language syntax.
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/