Home  >  Article  >  Backend Development  >  Summary of usage of some features in PHP7

Summary of usage of some features in PHP7

藏色散人
藏色散人forward
2022-01-10 17:19:591992browse

This article is provided by the PHP7 tutorial column to introduce you to some features of PHP7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Some features of PHP7

1.Usage of use

<?php

//  PHP 7 之前版本用法
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

// PHP 7+ 用法
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
?>

2.New serial number function unserialize Filter function

// 转换对象为 __PHP_Incomplete_Class 对象
$data = unserialize($foo, ["allowed_classes" => false]);

// 转换对象为 __PHP_Incomplete_Class 对象,除了 MyClass 和 MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]);

// 默认接受所有类
$data = unserialize($foo, ["allowed_classes" => true]);

3. Define constant array through define

<?php
define(&#39;ANIMALS&#39;, [
    &#39;dog&#39;,
    &#39;cat&#39;,
    &#39;bird&#39;
]);

  echo ANIMALS[1]; // 输出 "cat"
  define("GREETING","Hello you! How are you today?",TRUE); 
  echo constant("greeting"); //返回常量的值


?>

4. Ternary operator null merge

<?php
// 如果 $_GET[&#39;user&#39;] 不存在返回 &#39;nobody&#39;,否则返回 $_GET[&#39;user&#39;] 的值
$username = $_GET[&#39;user&#39;] ?? &#39;nobody&#39;;
// 类似的三元运算符
$username = isset($_GET[&#39;user&#39;]) ? $_GET[&#39;user&#39;] : &#39;nobody&#39;;
?>

The above is the detailed content of Summary of usage of some features in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete