PHP严格类型-奇怪的布尔行为

PHP严格类型-奇怪的布尔行为,php,Php,我有以下代码: <?php declare(strict_types=1); # test_1 with bool function test_1(bool $bool) { return $bool ? 'Yes' : 'No'; } # test_2 with boolean function test_2(boolean $bool) { return $bool ? 'Yes' : 'No';

我有以下代码:

<?php

    declare(strict_types=1);

    # test_1 with bool
    function test_1(bool $bool) {
        return $bool ? 'Yes' : 'No';
    }

    # test_2 with boolean
    function test_2(boolean $bool) {
        return $bool ? 'Yes' : 'No';
    }

    $value = false;

    # Why does this work ...
    echo test_1($value) . "<br>";

    # ... but this doesn't?
    echo test_2($value) . "<br>";


?>

PHP不允许在类型定义中使用boolean,因为关键字是bool。如果您键入boolean,它会将其解释为对类名的调用。什么是难以理解的

PHP仅支持
int
float
bool
string
array
类型。对类名的任何不同返回类型(如
boolean
)引用

<?php declare(strict_types=1)

class boolean {}

func testReturnBoolean(): boolean {
    // this function should return instance of
    // class "boolean", not bool type (true/false)
}

func testReturnBool(): bool {
    // this function should return true or false,
    // otherwise it throws an exception
}

func testReturnBoolOrNull():? bool {
    // this function should return true, false or null
    // otherwise it throws an exception
    // syntax :? string works since php 7.1
}

。。。。我已经读过了,但这是一个完全不同的案例。op似乎在不知情的情况下回答了自己的问题是的,但我觉得有点困惑,因为
有效。但我明白你的意思。太棒了,谢谢你。