php新增的特性,只是略微整理,并不完全。
一、php5.3添加的新特性
1、?:简化的三元运算符
?
|
1
2
3
|
<?php
$cur = $cur ? $cur : 1;
$cur = $cur ?: 1;
|
2、匿名函数
?
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
$test = function() {
echo 'test';
};
$test();
$arr = array(1, 2, 3, 4, 5);
$arr = array_map(function($n) {
return $n * 2;
}, $arr);
print_r($arr);
|
3、命名空间
?
|
1
2
3
4
5
6
7
8
9
10
|
<?php
namespace Test;
class Person {
public function say() {
echo 'hello';
}
}
$p = new \Test\Person();
$p->say();
|
4、支持延迟静态绑定
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
|
5、支持goto语句
?
|
1
2
3
4
5
6
7
|
<?php
for($i = 0; $i < 100; ++$i) {
if($i == 50) {
goto end;
}
}
end : echo $i;
|
6、新增两个魔术方法__callStatic()和__invoke()
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
class A {
public static function __callStatic($name, $arguments) {
echo '你调用了不存在的静态方法:', $name;
}
public function __invoke($i) {
echo '你把对象当函数调用了,还传了个值:', $i;
}
}
A::xxx();
$a = new A();
$a(6);
|
7、新增nowdoc语法
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
$name = 'test';
$a = <<< 'TEST'
测试$name
TEST;
$b = <<< TEST
测试$name
TEST;
echo $a;
echo $b;
|
8、类外也可用const定义常量
9、http状态码在200-399范围内均被认为访问成功。
10、支持动态调用静态方法
?
|
1
2
3
4
5
6
7
8
9
|
<?php
class A {
public static function test() {
echo 'test';
}
}
$cName = 'A';
$mName = 'test';
$cName::$mName();
|
二、php5.4添加的特性
1、内置一个简单的Web服务器
2、数组的简化写法
?
|
1
2
3
4
5
6
|
<?php
$arr = array(1, 2, 3);
$arr = array('a' => 1, 'b' => 2);
$arr = [1, 2, 3];
$arr = ['a' => 1, 'b' => 2];
|
3、新增支持对函数返回数组的成员访问解析
?
|
1
2
3
4
5
|
<?php
function getArr() {
return array(1, 2, 3, 4, 5);
}
echo getArr()[2];
|
4、新增实例化时访问类成员
?
|
1
2
3
4
5
6
7
|
<?php
class A {
public function test() {
echo 'test';
}
}
(new A())->test();
|
5、新增对短标签的支持,无论是否设置short_open_tag。
6、session提供上传进度支持,$_SESSION['upload_progress_name']。
7、新增加了$_SERVER['REQUEST_TIME_FLOAT'],用来统计服务请求时间。
8、默认使用mysqlnd,mysqli,pdo默认使用mysqlnd本地库。
9、让json更懂中文
?
|
1
2
3
4
5
6
|
<?php
$arr = array(
'name' => '哈哈',
'age' => 25,
);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
|
10、新增二进制直接量
?
|
1
2
3
|
<?php
$bin = 0b101;
echo $bin;
|
11、默认default_charset从ISO-8859-1已经变为UTF-8
12、新增Traits
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
trait A {
public function fly() {
echo '会飞';
}
}
trait B {
public function swim() {
echo '游泳';
}
}
class People {
use A, B;
}
(new People())->fly();
|
三、php5.5新增的特性
1、empty支持传入一个任意表达式,不仅是一个变量
?
|
1
2
3
4
5
6
7
|
<?php
var_dump(empty(3 * 3));
function test() {
return false;
}
var_dump(empty(test()));
|
2、新增密码哈希API,password_hash
3、新增生成器
?
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
$arr = function() {
for($i = 0; $i < 10; ++$i) {
yield $i;
}
};
$data = $arr();
foreach($data as $v) {
echo $v;
}
|
4、新增finally关键字
?
|
1
2
3
4
5
6
7
8
|
<?php
try {
throw new Exception('我是异常');
} catch(Exception $e) {
echo $e->getMessage();
} finally {
echo 'finally';
}
|
5、新增boolval()函数
6、新增array_column()函数
?
|
1
2
3
4
5
6
7
8
9
10
|
<?php
$arr = array(
array('name' => 'test1', 'age' => 22),
array('name' => 'test2', 'age' => 23),
array('name' => 'test3', 'age' => 24),
);
print_r(array_column($arr, 'name'));
print_r(array_column($arr, 'name', 'age'));
|
7、foreach支持list()
?
|
1
2
3
4
5
6
7
8
9
|
<?php
$arr = [
[3, 3],
[4, 4],
[5, 5],
];
foreach($arr as list($a, $b)) {
echo $a, '-', $b, '<br>';
}
|
8、增加了opcache扩展
四、php5.6新增的特性
1、可以使用表达式定义常量
?
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
const ONE = 1 * 1;
class A {
const TWO = 1 * 2;
const THREE = self::TWO + 1;
public function test($i = self::THREE + ONE) {
echo $i;
}
}
(new A())->test();
|
2、使用...定义变长函数参数
?
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
function total(...$nums) {
$total = 0;
foreach($nums as $num) {
$total += $num;
}
return $total;
}
echo total(1, 2, 3, 4, 5);
$arr = [3, 4, 5, 6];
echo total(...$arr);
|
3、使用**进行幂运算
?
|
1
2
3
4
5
|
<?php
echo 2 ** 4;
$a = 2;
$a **= 4;
echo $a;
|
4、use function和use const
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
namespace A {
const PI = 3.14;
function test() {
echo 'test';
}
}
namespace B {
use function \A\test;
use const \A\PI;
echo PI;
test();
}
|
5、加入hash_equals()函数,以恒定的时间消耗来进行字符串比较,以避免时序攻击。
6、加入__debugInfo()
当使用var_dump()输出对象的时候,可以用来控制要输出的属性和值。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
class A {
protected $a = 1;
protected $b = 2;
protected $c = 3;
public function __debugInfo() {
return array(
'a' => $this->a,
);
}
}
var_dump((new A()));
|
五、php7新增的特性
1、??运算符(NULL合并运算符)
?
|
1
2
|
<?php
$page = $_GET['page'] ?? 1;
|
2、标量类型声明
3、函数返回值类型声明
?
|
1
2
3
4
5
6
7
8
9
|
<?php
declare(strict_types=1);
function add(int $num1, int $num2) : int {
return $num1 + $num2;
}
echo add(2, 3);
echo add(2.0, 3.0);
|
4、匿名类
?
|
1
2
3
4
5
6
|
<?php
(new class {
public function test() {
echo 'test';
}
})->test();
|
5、通过define()定义常量数组
?
|
1
2
3
|
<?php
define('ARR', ['a', 'b', 'c']);
echo ARR[2];
|
|