Re: Request for comments - new PHP feature: return typing

From: Date: Wed, 26 Jun 2013 16:33:38 +0000
Subject: Re: Request for comments - new PHP feature: return typing
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Johannes,

2013/6/26 Johannes Schlüter <[email protected]>:
> So a function author doesn't trust himself and therefore we change the
> language syntax?

In the following example:

class Foo {
    public function bar($input) {
        $return = Baz::foo($input);
        if (!$return instanceof ReturnType)
            trigger_error("Cannot divide by zero", E_USER_ERROR); //
or throwing some RuntimeException

        return $return;
    }
}

Baz class can be from a 3rd party software and I may want to enforce
the fact that my Foo::bar() method returns an instance of ReturnType
or throw an exception or trigger an error.
This is moez easily written with this proposition:

class Foo {
    public function <ReturnType> bar($input) {
        return Baz::foo($input);
    }
}

With the same reasoning you had, you would avoid using parameter type
hinting and interfaces for your own project if there's no other people
using it.


Thread (35 messages)

« previous php.internals (#67900) next »