简体   繁体   中英

PHP type hint none

I've build iterator in PHP which has a method that returns an array property when iterator is not valid:

class MyIterator... {

    private array $arrayProperty = [];

    public function returnArray():array {

        // Appending values to that array
        // next,current stuff...

        if (! $this->valid()){
            return $this->arrayProperty
    }
}

Everything worked fine until I've typehinted return type array. Now im getting TypeError: Return value must be of type array, none returned. is there a way how to type hint none?

I will presume the code is this:

public function returnArray():array {

    // Appending values to that array
    // next,current stuff...

    if (! $this->valid()){
        return $this->arrayProperty;
    }
}

In this case, indeed you are returning nothing. Typehint so and make it explicit:

public function returnArray(): ?array {

    // Appending values to that array
    // next,current stuff...

    if (! $this->valid()){
        return $this->arrayProperty;
    }
    return NULL;
}

In PHP, any function that doesn't explicitly return a value implicitly returns null . Consider the following:

function a() {
    // do nothing
}
var_dump( a() ); // null

function b($condition) {
    if ( $condition ) {
       return 42;
    }
}
var_dump( b(true) ); // int(42)
var_dump( b(false) ); // null

So your function returns either an array or null , which can be declared with a nullable type : ?array

For clarity, you might want to explicitly write out the return null

class MyIterator... {

    private array $arrayProperty = [];

    public function returnArray(): ?array {

        // Appending values to that array
        // next,current stuff...

        if (! $this->valid()){
            return $this->arrayProperty;
        }
        else {
            return null;
        }
    }
}

I guess I feel too strongly about my comment under the question to leave it there and hope that it will be read.

I don't think I have ever made a nullable array return type. Whatever script is calling the method is expecting iterable data to be returned not a scalar type. If there is data, the array is populated. If there is no data, the array is empty. If there is something significantly afoul, throw an exception. I recommend never using ?array for simplicity of subsequent data processing.

Both null and an empty array are "falsey" values. Returning an empty array is perfectly suitable for falsey checking as well as instantly feeding into a loop or native array function.

I'm considering this an XY Problem . Refactor this method to never return null .

class MyIterator
{
    private array $arrayProperty = [];

    public function accumulateViolations(): array
    {
        $this->valid(); // potentially populates $this->arrayProperty
        return $this->arrayProperty;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM