// get_class() 函数不再接受 null var_dump(get_class(null)); // PHP72 // Warning: get_class() expects parameter 1 to be object // bool(false) // // PHP71 // Warning: get_class() called without object from outside a class // bool(false) // // PHP80 // Fatal error: Uncaught TypeError: get_class(): Argument #1 ($object) must be of type object, null given
// 计算非可数类型(non-countable)时发出警告 var_dump( count(null), // NULL is not countable count(1), // integers are not countable count('abc'), // strings are not countable count(new stdclass) // objects not implementing the Countableinterface are not countable ); // PHP72 // Warning: count(): Parameter must be an array or an object that implements Countable // // PHP71 // 无 Warning // // PHP80 // Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array
Heredoc/nowdoc syntax improvements and a bunch of legacy code deprecations.
New Features 7.3
// Heredoc Nowdoc 不再需要后跟分号或换行符 // 结束标记可以缩进,结束时所引用的标识符必须在该行的第一列 $values = [<<<END a b c END, 'd e f']; var_dump($values); // PHP73 array(2) { // [0]=> // string(13) "a // b // c" // [1]=> // string(5) "d e f" // } // // PHP72 Parse error: syntax error, unexpected end of file
echo<<<END a b c END; // PHP73 // a // b // c // // PHP72 Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)
echo<<<END a b c END; // PHP73 Parse error: Invalid body indentation level
// Gets the first key of an array array_key_first(array$array): int|string|null // Gets the last key of an array array_key_last(array$array): int|string|null // Get the system's high resolution time // [seconds, nanoseconds] int(64 位平台)或 float(32 位平台) hrtime(bool$as_number = false): array|int|float|false // 验证变量的内容是否为 countable 值 // return is_array($value) || $value instanceof Countable || $value instanceof ResourceBundle || $value instanceof SimpleXmlElement; is_countable(mixed$value): bool
Backward Incompatible Changes 7.3
// Continue Targeting Switch 问题警告 $foo = 0; while ($foo < 10) { switch ($foo) { case"baz": continue; } $foo++; } // PHP73 Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? // PHP72 ok
// 有限的 Limited 返回类型协变和参数类型逆变 classA{} classBextendsA{} classProducer{ publicfunctionmethod(): A{} } classChildProducerextendsProducer{ publicfunctionmethod(): B{} } // PHP74 ok // PHP73 Fatal error: Declaration of ChildProducer::method(): B must be compatible with Producer::method(): A
// 以数组形式访问非数组,将会抛出 notic // null, bool, int, float or resource $i = 12; $i["a"]; // PHP74 Notice: Trying to access array offset on value of type int // PHP73 ok // PHP80 Warning: Trying to access array offset on value of type int
Deprecated Features 7.4
// 嵌套的三元运算必须明确地使用括号来指示运算的顺序 var_dump(1 ? 2 : 3 ? 4 : 5); // PHP74 Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` // PHP73 int(4)