2016-02-18 9 views
14

Mam problem z typami zwracania w php7, szczególnie "void".php7 void return type nie działa?

Działa z wszystkimi innymi typami, int, string, null, bool, class objects.

, ale gdy używam pustki oczekuję, że zwrócę instancję obiektu, ale w rzeczywistości nie powinien oczekiwać żadnego zwrotu, ponieważ to, za co jest pustka.

uwaga: Używam PHP 7.0.3

oto kod:

public static function setResponseCode(int $code) : void 
    { 
     http_response_code($code); 

    } 

i komunikat o błędzie:

Uncaught TypeError: Return value of CodeBase\HttpRequester::setResponseCode() must be an instance of void, none returned in /var/www/html/src/HttpRequester.php:86 Stack trace: #0 /var/www/html/index.php(103): CodeBase\HttpRequester::setResponseCode(500) #1 {main} thrown in /var/www/html/src/HttpRequester.php on line 86

+1

Spróbuj dodać '' return; co jest ważne.Ponieważ 'http_response_code' zwraca wartość, może się myli i myśli, że wynik jest zwracany – RiggsFolly

+3

@RiggsFolly lub może nie ma czegoś takiego (jeszcze) jako nieważny typ zwrotu :) – PeeHaa

+0

@PeeHaa Ah tak zapomniałem Czytałem Dokumenty RFC – RiggsFolly

Odpowiedz

21

Odpada typy zwracane są dla PHP 7.1 (który nie został jeszcze zwolniony, kiedy o to prosiłeś). From the RFC

Version: 0.2.1
Date: 2015-02-14 (v0.1, later withdrawn), 2015-10-14 (v0.2, revival)
Author: Andrea Faulds, [email protected]
Status: Implemented (PHP 7.1)

-4

Dlaczego nie wystarczy użyć

function printLn($a) { 
    echo $a; 
    return;} 

To jest taka sama jak pustki.

Można nawet usunąć zwrot z tylko echo

+4

Dlaczego? Ponieważ ideą deklaracji typu return jest unikanie zwracania jakichkolwiek innych zmiennych w tej funkcji. Co, jeśli inny programista będzie chciał połączyć niektóre parametry i zwrócić to, co chce? –

7

No nie ma, dopóki PHP 7.1. W przypadku PHP 7.0 należy całkowicie pominąć typ zwracany dla funkcji/metod void.

function printLn($a) { 
    echo "$a\n"; 
} 

Niestety, wtedy nie masz bezpieczeństwa typu dla tej funkcji/metody, a nie TypeError zostanie wyrzucony jeśli zaczniesz powrocie czegoś od niego.

szczęście PHP 7.1 fixes this:

Support for a new void return type is added. It requires that a function not return any value.

to poprawna składnia PHP 7,1:

function should_return_nothing(): void { 
    return 1; // Fatal error: A void function must not return a value 
} 

ten został przesunięty w the proposal that created return type hints:

We keep the current type options. Past proposals have suggested new types such as void, int, string or scalar; this RFC does not include any new types. Note that it does allow self and parent to be used as return types. ...

Future Work

Ideas for future work which are out of the scope of this RFC include:

  • Allow functions to declare that they do not return anything at all (void in Java and C)

NULL jest n ot dozwolony jako typ zwrotu.

1

tl; dr

Zwraca typ void prace od PHP 7.1, która jest już dostępna.

składnia Praca jest:

<?php 
function procedure(): void 
{ 
// return 'will not work'; 
} 

echo procedure(); 
Powiązane problemy