<?php
function answer(): ?int {
return null; //ok
}
function answer(): ?int {
return 42; // ok
}
function say(?string $msg) {
if ($msg) {
echo $msg;
}
}
<?php
interface Fooable {
function foo(?Fooable $f);
}
<?php
function foo_nullable(?Bar $bar) {}
foo_nullable(new Bar); // 可行
foo_nullable(null); // 可行
foo_nullable(); // 不可行
<?php
// 5.4 之前
$array = array(1, 2, 3);
$array = array("a" => 1, "b" => 2, "c" => 3);
// 5.4 及之后
$array = [1, 2, 3];
$array = ["a" => 1, "b" => 2, "c" => 3];
<?php list($a, $b, $c) = $array;
<?php [$a, $b, $c] = $array;
<?php ["a" => $a, "b" => $b, "c" => $c] = $array;
<?php
foreach ($points as ["x" => $x, "y" => $y]) {
var_dump($x, $y);
}
<?php // 不合法 list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; // 不合法 [list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; // 合法 [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
<?php $array = ["a" => 1, "b" => 2, "c" => 3]; ["a" => $a, "b" => $b, "c" => $c] = $array;
<?php $a = $array['a']; $b = $array['b']; $c = $array['c'];
<?php list($a, $b) = [1 => '1', 2 => '2'];
<?php list(1 => $a, 2 => $b) = [1 => '1', 2 => '2'];
<?php // Parse error: syntax error, ... list($unkeyed, "key" => $keyed) = $array;
<?php
$points = [
["x" => 1, "y" => 2],
["x" => 2, "y" => 1]
];
list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points;
$points = [
"first" => [1, 2],
"second" => [2, 1]
];
list("first" => list($x1, $y1), "second" => list($x2, $y2)) = $points;
<?php
$points = [
["x" => 1, "y" => 2],
["x" => 2, "y" => 1]
];
foreach ($points as list("x" => $x, "y" => $y)) {
echo "Point at ($x, $y)", PHP_EOL;
}
<?php
function should_return_nothing(): void {
return 1; // Fatal error: A void function must not return a value
}
<?php
function lacks_return(): void {
// valid
}
function returns_nothing(): void {
return; // valid
}
<?php
function returns_one(): void {
return 1; // Fatal error: A void function must not return a value
}
function returns_null(): void {
return null; // Fatal error: A void function must not return a value
}
<?php
function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type
}
<?php
class Foo
{
public function bar(): void {
}
}
class Foobar extends Foo
{
public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void
}
}
<?php
class Token {
// 常量默认为 public
const PUBLIC_CONST = 0;
// 可以自定义常量的可见范围
private const PRIVATE_CONST = 0;
protected const PROTECTED_CONST = 0;
public const PUBLIC_CONST_TWO = 0;
// 多个常量同时声明只能有一个属性
private const FOO = 1, BAR = 2;
}
<?php
interface ICache {
public const PUBLIC = 0;
const IMPLICIT_PUBLIC = 1;
}
<?php
class testClass {
const TEST_CONST = 'test';
}
$obj = new ReflectionClass( "testClass" );
$const = $obj->getReflectionConstant( "TEST_CONST" );
$consts = $obj->getReflectionConstants();
<?php
try {
// Some code...
} catch (ExceptionType1 $e) {
// 处理 ExceptionType1
} catch (ExceptionType2 $e) {
// 处理 ExceptionType2
} catch (\Exception $e) {
// ...
}
<?php
try {
// Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
// 对于 ExceptionType1 和 ExceptionType2 的处理
} catch (\Exception $e) {
// ...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有