Void as return type

后端 未结 5 836
忘了有多久
忘了有多久 2020-12-05 13:11

I was testing return types with PHP 7.

I\'ve created a simple script to test return types of PHP 7:



        
5条回答
  •  無奈伤痛
    2020-12-05 13:16

    The voidreturn type was accepted for php 7.1. So it will come in the future.

    Some examples on how it will work:

    function should_return_nothing(): void {
        return 1; // Fatal error: A void function must not return a value
    }
    
    function returns_null(): void {
        return null; // Fatal error: A void function must not return a value
    }
    function lacks_return(): void {
        // valid
    }
    function returns_nothing(): void {
        return; // valid
    }
    

    See the RFC from Andrea Faulds for more info!

提交回复
热议问题