Is it possible to write strictly typed PHP code?

后端 未结 9 768
陌清茗
陌清茗 2020-12-14 15:34

For example, is it possible to write code like this:

int $x = 6;
str $y = \"hello world\";
bool $z = false;
MyObject $foo = new MyObject();

相关标签:
9条回答
  • 2020-12-14 15:52

    Something you might try in order to simulate a poor man's strict type checking is using assert() to force the output to be of a particular type before you return it:

    /**
     * Get Balance
     *
     * @return int
     */
    function getBalance()
    {
        /* blah blah blah */
       $out = 555; //Or any numeric value
       assert('is_int($out)');
       return $out;
    }
    

    So you keep your assertions active all throughout development and testing, sort of like the checks the compiler does at compile-time.

    Granted, the assert() page is keen to assert that you shouldn't use assertions to check input parameters, but rather use normal conditionals to check them.

    This answer had what I thought was a good rule:

    The rule of thumb which is applicable across most languages (all that I vaguely know) is that an assert is used to assert that a condition is always true whereas an if is appropriate if it is conceivable that it will sometimes fail.

    If you're simulating strict type-checking (writing your code to viciously maintain types; not trying to validate input from the outside), then you ought to be certain what the type is unless you've made a mistake.

    Update:

    There's also this: http://hacklang.org/ Facebook's PHP-based language with static typing.

    0 讨论(0)
  • 2020-12-14 15:56

    You could use h2tp transpiler to transpile HACK code to PHP:

    You can do this thanks to Facebook Team and HACK Language.

    Visit http://hacklang.org and http://hhvm.com for more info.

    If you want to code directly in Hack + HHVM environment you can also use Facebook's internal IDE Nuclide

    0 讨论(0)
  • 2020-12-14 15:59

    In PHP 7 are implemented "Scalar Type Declarations", e.g.:

    public function getBalance(): int {
        return 555;
    }
    

    You need to declare, that you will use strict types:

    <?php
        declare(strict_types=1);
    
        function sum(int $a, int $b): int {
            return $a + $b;
        }
    
        sum(1, 2);
    ?>
    

    More information: https://wiki.php.net/rfc/scalar_type_hints_v5

    0 讨论(0)
  • 2020-12-14 16:01

    No. That syntax will not work.

    You could, theoretically, come up with a system of objects that enforced their own sort of strict typing, but it wouldn't perform and ...why would you want to, anyway?

    If you need strict typing, use a strictly typed language.

    0 讨论(0)
  • 2020-12-14 16:03

    Perhaps you should try this PHP extension https://github.com/krakjoe/strict. Support for the following types is introduced:

    • string
    • integer, int
    • float, double
    • boolean, bool
    • resource
    0 讨论(0)
  • 2020-12-14 16:04

    PHP is not strictly typed, so no. That said, it does support limited type hinting on functions - that's as close as it gets.

    0 讨论(0)
提交回复
热议问题