Created
May 21, 2018 08:43
-
-
Save HavenShen/4d64e4960f685558669a0d728905ef38 to your computer and use it in GitHub Desktop.
在 PHP 5 中只能是类名、接口、array 或者 callable,新增 string、int、float 和 bool。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| 标量类型声明 | |
|-------------------------------------------------------------------------- | |
| | |
| 在 PHP 5 中只能是类名、接口、array 或者 callable,新增 string、int、float 和 bool。 | |
| | |
*/ | |
// PHP 7 之前 | |
class User | |
{ | |
public function update($id, array $properties) | |
{ | |
if (! is_int($id)) { | |
throw new Exception; | |
} | |
} | |
} | |
$user = new User; | |
$user->update('Haven Shen', []); | |
// PHP 7 之后 | |
class User | |
{ | |
public function update(int $id, array $properties) | |
{ | |
// ...update | |
} | |
} | |
$user = new User; | |
$user->update('Haven Shen', []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment