Return Typehint

Return types are the types used for the return values of a method or function.

They are the same as the argument types, with a few addition :

  • void, which means that no value is returned. (No usage of return, or return with no explicit value)

  • never, which means that the function won’t return : either it will kill the application, or it will throw an exception.

Return types are ignored when the method throws an exception.

Return type are covariant : they get more or equally precise with each new child generation.

<?php

function foo() : bool {
    if (rand(0,1)) {
        return true;
    } else {
        throw new Exception('e');
    }
}

?>

Documentation

Related : Type System, Return, Covariance