FAQ
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

Search Discussions

  • Stanislav Malyshev at Feb 5, 2007 at 5:51 pm

    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 at Feb 5, 2007 at 6:37 pm

    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 at Feb 5, 2007 at 6:48 pm

    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/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedFeb 5, '07 at 5:57a
activeFeb 5, '07 at 6:48p
posts4
users2
websitephp.net

People

Translate

site design / logo © 2022 Grokbase