Martin
Martin

Reputation: 2785

Type-hinting return value/functions in PHP

I did some research and came over this RFC which discusses type hinting for functions in PHP: https://wiki.php.net/rfc/returntypehint2

For example

public string getName()
{
    return 'martinmine';
}

Would be valid PHP code. Returning for example an array would yield an error in this case. Does anyone know the status on the RFC or if it got removed/added? I cannot seem to be able to make this function above work. I am using PHP 5.5.9.

Upvotes: 2

Views: 1763

Answers (1)

Musa Haidari
Musa Haidari

Reputation: 2267

Type hinting for return types are postponed to PHP 7 (as of this article), but the syntax is different than of the old purpose one:

function foo(): array {
    return [];
}

But it is already available in PHP 5 for parameters of type class of a method or function(as of this article):

public function test_array(array $input_array) {
    print_r($input_array);
}

Upvotes: 2

Related Questions