On Wed, Jun 26, 2013 at 5:50 AM, Florin Patan <florinpa...@gmail.com> wrote:

> Hello PHP internals,
>
>
> Currently PHP doesn't support what could be a good feature for code
> quality, return typing (or if you prefer to call it: return type
> hinting).
>
> Since the procedure says that before any real RFC should be done, the
> interest for this topic should be gauged, I've drafted something here:
> https://gist.github.com/dlsniper/5863012
>
> The goals for this discussion are:
> - gauge the interest in having such a feature in PHP.NEXT
> - find weak spots in the draft
> - find potential problems with the suggested way of doing the
> implementation
> - determine if the advantages would outweigh the disadvantages and
> this should go forward as a RFC
>
> As you can see, the draft doesn't include any patch and since my C
> skills are mostly nonexistent, I currently can't provide one
> unfortunately. I would however love to do it with proper guidance from
> someone that could spare some of his/her time to help me out.
>
> This said, thank you very much for your time, consideration and
> feedback on this subject.
>
>
> Best regards,
> Florin
> ----
> Florin Patan
> https://github.com/dlsniper
> http://www.linkedin.com/in/florinpatan
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
PHP really doesn't need function return type declaration since PHP doesn't
even support variable declaration. This would make sense in a language like
C or C++ where the compiler can do static analyses to determine potential
errors before compiling.

Since PHP is a loosely typed dynamic language, however, the fact that a
function can return something of any type is quite liberating and it turns
out that you don't need the static analyses to write better code with PHP,
because most of the errors static analyses helps you with aren't really the
things you are most concerned with. For example consider the following bug:

<?php
function userTimezone($userId, PDO $db) {
    $result = [];
    if (!($stmt = $db->prepare('SELECT `timezone` FROM users WHERE
users.`id` = ?'))) {
        return false;
    }
    if ($stmt->execute([$userId])) {
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    return array_pop($result);
}
?>

If in the above code PDO::prepare returns false (upon failure) for any
reason $stmt will not be of the expected type. In that case the function
returns a boolean false to indicate failure (or it can throw an exception
as well). If the Exception is throw the function's return value is still
implicitly null. However, the return type of the function isn't what we
actually care about here, because it doesn't solve our real problem. The
real problem we care about is the fact that the function failed and
couldn't keep its promise. If we force the function to only return an array
we can't tell the difference between an empty result set or a failure case
unless we strictly stick to exceptions. In none of those cases was the
function's return value type hint helpful.

Reply via email to