简体   繁体   中英

How can I make PHP7 parser to force return types?

Is there any directive in the recent PHPs (maybe similar to that of declare( strict_types = 1 ); ) that tells the PHP interpreter to "force" return types in the functions, and if there's no return type, it fails due to a parse/syntax error?

I'd like that having this in the code:

public function add( int $a, int $b ) : int
{
    return $a + $b;
}

is allowed but this one:

public function add( int $a, int $b )
{
    return $a + $b;
}

is forbidden by the interpreter resulting in a parse error so the script never gets executed even if the function is not called.

I mean:

It is not that I want to "force a return type in a certain function" (I already do that). What I want is that the configuration "forces me to force return types in all the functions of a file or project".

I had a look at PHP RFC: Return Type Declarations and there is no way (for now and for future release) to do such a thing ...

It's the same for declare(strict_types=1) ( PHP RFC: Scalar Type Declarations ) which only ensure that the existing declarations are strictly typed checked.

The short answer to your question is No. PHP doesn't have anything to force you to define your return type. It also doesn't have anything to force you to define types for your arguments either -- strict_types = 1 forces passed values to be of the correct type for the method if the type is defined, but you can still write methods with arguments that don't define a type.

It is possible that PHP may make changes in a future release along the lines you're looking for, but it isn't on the cards for any version right now.

In the meanwhile, I would suggest using a code checking tool to help you enforce this kind of detail in your code. A tool like PHP Code Sniffer will be a good start. You can add it to your IDE so that you get warnings in real time when your code doesn't meet your defined standards, and you can add it to your workflow so that it prevents code from being committed and/or deployed if it fails the test.

What exactly is your problem with not having a forced fixed return type? I like the lazy way of coding with PHP. I could be cumbersome in large projects to find a mistyped variable name or wrong typed return value I admit. But there are remedies to that.

If a caller expects a definitive type it can (must!) test it anyway. This could be done by converting the result to the requested type and throwing an exception on failure as shown in PHP String-to-Integer Exception . Or in case of integer simply using is_numeric , is_nan , is_real etc.. You can put those tests in member functions for each type in the base object or even use a global singleton tester object with those member functions etc..

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