最全的PHP7新特性

一、数组和字符串直接表达式

<?php
echo "hello world"[4];

二、新的密码加密函数

  • 该算法弊端在于会增加了数据库字段的容量,因为加密后的hash太长

<?php
$str = 'i am password';
//用户加密
$hash = password_hash($str, PASSWORD_BCRYPT);
//用于验证加密是否正确
password_verify($str, $hash);

三、异常和finally关键字

  • 异常处理语句由原来的 try-catch 新增了一个 finally语句块,适合用于写哪些不管是否异常最终都会执行的代码

<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test;', 'root', '123456');
    $msg = "开始正常执行";
} catch (Error $e) {
    $msg = "错误:" . $e->getMessage();
} catch (Exception $e) {
    $msg = "异常:" . $e->getMessage();
} finally {
    file_put_contents('error.log', $msg, FILE_APPEND);
}

四、生成器( Generator )

  • 生成器是专门了 处理大数组(大数据) 和 协程通信 而出现

<?php
function xrange($start, $end, $step=1)
{
    for ($i=$start; $i<$end; $i+=$step)
    {
        yield $i;
    }
}

foreach (xrange(0, 1000) as $item)
{
    echo $item;
}

五、函数参数列表支持接收可变数量参数

<?php
function test(...$args)
{
    print_r($args);
}

test(1,2,3,4,5);

六、幂运算,用来替代pow函数

<?php
$pow = pow(2,10);
$replace = 2 ** 10;
var_dump($replace == $pow);

七、定义数组常量

<?php
const STATUS = [
    'ERROR' => 0,
    'SUCCESS' => 1
];

STATUS['ERROR'];

八、类型严格模式

  • 函数参数类型限制
<?php
class test1 {}
class test2 {}

function fun(test1 $obj) {}

$t1 = new test1();
$t2 = new test2();

fun($t1);
fun($t2); //会报错
  • 类型严格模式
<?php
function fun(int $a) {}

fun('string'); //传入非int类型会报错
  • 函数返回值类型限定
<?php
function test():array
{
    return 888;
}

test(); //会报错,约束返回数组,但是返回一个int

九、新增运算符

  • 飞船运算符
<?php
$num1 = 100;
$num2 = 200;
$num3 = 200;

echo $num1 <=> $num2; //左边比右边小, -1
echo $num3 <=> $num1; //左边比右边大, 1
echo $num2 <=> $num3; //左右相等, 0
  • null合并运算符
<?php
echo $_GET['name'] ?? 'felix';

十、除法除整商

<?php
echo intdiv(14, 3); //返回整数部分

十一、匿名类

<?php
$nm = new class {
    public function say() {
        echo "i am saying";
    }
};
$nm->say();

十二、IntlChar类

  • 需要开启php_intl扩展
<?php
echo IntlChar::ord('A'); //获取A的ascii码65

十三、Closure::call

  • 允许我们把匿名函数临时挂到指定对象中,充当这个对象的类方法
<?php
class Test {
    private $name = 'felix';
}

$test = new Test();

$say = function() {
    echo $this->name;
};

$say->call($test);
$say();

十四、命名空间成员的批量导入

<?php
use App\Course\{
    Common\Lesson,
    Base\Teacher
};

new Lesson();
new Teacher();

十五、捕获错误信息

  • php7.0以后,PHP是不支持捕获错误,只能捕获异常
​<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test;', 'root', '123456');
    $msg = "开始正常执行";
} catch (Error $e) {
    $msg = "错误:" . $e->getMessage();
} catch (Exception $e) {
    $msg = "异常:" . $e->getMessage();
} finally {
    file_put_contents('error.log', $msg, FILE_APPEND);
}

十六、session_start()中可动态设置项覆盖配置项

  • session_start 原来的作用用来开启session

<?php
date_default_timezone_set("PRC");

session_start([
    'save_path' => 'F:\Session',
    'name' => 'FELIXSESSID',
    'cookie_lifetime' => 3600
]);

十七、生成器支持返回值

<?php
function builder()
{
    yield '第1次调用生成器';
    yield '第2次调用生成器';
    yield '第3次调用生成器';
    yield '第4次调用生成器';
    return 'success';
}

$builder = builder();

foreach ($builder as $item)
{
    echo $item . '<br />';
}

//如果7以下,一定会报错
if ($builder->getReturn() == 'success') {
    echo "程序执行完毕";
}

十八、生成器的委托

  • 生成器的委托操作,主要是把一个复杂的生成器分离成若干简单的生成器
<?php
function builder()
{
    yield '奥特之父发出指定';
    if (mt_rand(0, 1) == 0) {
        yield from tl();
    } else {
        yield from dj();
    }
}

function tl()
{
    yield "泰罗你去打小怪兽";
}

function dj()
{
    yield "迪迦你去打小怪兽";
}

$builder  = builder();

foreach ($builder as $item)
{
    echo "{$item}<br/>";
}

猜你喜欢

转载自blog.csdn.net/weixin_41635750/article/details/108266870