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

Summary of usage of some features in PHP7

藏色散人
Release: 2023-02-18 09:58:01
forward
2106 people have browsed it

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};
?>
Copy after login

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]);
Copy after login

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"); //返回常量的值


?>
Copy after login

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;;
?>
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template