Home > Backend Development > PHP7 > Introduce some features of PHP7

Introduce some features of PHP7

coldplay.xixi
Release: 2023-02-17 17:44:01
forward
2774 people have browsed it

Introduce some features of PHP7

Related learning recommendations: php programming (video)

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. The serial number function unserialize has a new filtering 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 a 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

If you want to learn more about programming, please pay attention to the php training column!

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

Related labels:
source:phpxs.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