> For the complete documentation index, see [llms.txt](https://php7.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php7.shujuwajue.com/php-70x-xin-te-xing/qi-ta-te-xing-he-bian-geng/bu-5145-qi-ta-te-xing-he-bian-geng.md).

# 补充\*其他特性和变更

## 目录:

* Unicode codepoint 转译语法
* Closure::call()
* IntlChar
* 预期
* 生成器可以返回表达式
* Generator delegation
* preg\_replace\_callback\_array()
* CSPRNG Functions
* 可以使用list()函数来展开实现了ArrayAccess接口的对象
* 允许在克隆表达式上访问对象成员
* 可以使用关键词作为方法名（链式操作）

## Unicode codepoint 转译语法

这接受一个以16进制形式的 Unicode codepoint，并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint，并且开头的 0 是可以省略的。

```php
echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";
```

以上例程会输出：

```
ª
ª (same as before but with optional leading 0's)
香
```

## **Closure::call()**

**Closure::call()**&#x73B0;在有着更好的性能，简短干练的暂时绑定一个方法到对象上闭包并调用它。

```php
<?php
class A {private $x = 1;}

// PHP 7 之前版本的代码
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // 中间层闭包
echo $getX();

// PHP 7+ 及更高版本的代码
$getX = function() {return $this->x;};
echo $getX->call(new A);
```

以上例程会输出：

```
1
1
```

## IntlChar

新增加的[IntlChar](http://php.net/manual/zh/class.intlchar.php)类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。

```php
<?php

printf('%x', IntlChar::CODEPOINT_MAX);
echo IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));
```

以上例程会输出：

```
10ffff
COMMERCIAL AT
bool(true)
```

若要使用此类，请先安装[Intl](http://php.net/manual/zh/book.intl.php)扩展

## 预期

[预期](http://php.net/manual/zh/function.assert.php#function.assert.expectations)是向后兼用并增强之前的[assert()](http://php.net/manual/zh/function.assert.php)的方法。 它使得在生产环境中启用断言为零成本，并且提供当断言失败时抛出特定异常的能力。

老版本的API出于兼容目的将继续被维护，[assert()](http://php.net/manual/zh/function.assert.php)现在是一个语言结构，它允许第一个参数是一个表达式，而不仅仅是一个待计算的[string](http://php.net/manual/zh/language.types.string.php)或一个待测试的[boolean](http://php.net/manual/zh/language.types.boolean.php)。

```php
<?php
ini_set('assert.exception', 1);

class CustomError extends AssertionError {}

assert(false, new CustomError('Some error message'));
?>
```

以上例程会输出：

```
Fatal error: Uncaught CustomError: Some error message
```

关于这个特性的完整说明，包括如何在开发和生产环境中配置它，可以在[assert()](http://php.net/manual/zh/function.assert.php)的[expectations section](http://php.net/manual/zh/function.assert.php#function.assert.expectations)章节找到。

## 生成器可以返回表达式

此特性基于 PHP 5.5 版本中引入的生成器特性构建的。 它允许在生成器函数中通过使用*return*语法来返回一个表达式 （但是不允许返回引用值）， 可以通过调用*Generator::getReturn()*&#x65B9;法来获取生成器的返回值， 但是这个方法只能在生成器完成产生工作以后调用一次。

```php
<?php

$gen = (function() {
    yield 1;
    yield 2;

    return 3;
})();

foreach ($gen as $val) {
    echo $val, PHP_EOL;
}

echo $gen->getReturn(), PHP_EOL;
```

以上例程会输出：

```
1
2
3
```

在生成器中能够返回最终的值是一个非常便利的特性， 因为它使得调用生成器的客户端代码可以直接得到生成器（或者其他协同计算）的返回值， 相对于之前版本中客户端代码必须先检查生成器是否产生了最终的值然后再进行响应处理 来得方便多了。

## Generator delegation

现在，只需在最外层生成其中使用 [*yield from*](http://php.net/manual/zh/language.generators.syntax.php#control-structures.yield.from)， 就可以把一个生成器自动委派给其他的生成器，**Traversable** 对象或者 [array](http://php.net/manual/zh/language.types.array.php) 。

```php
<?php

function gen()
{
    yield 1;
    yield 2;

    yield from gen2();
}

function gen2()
{
    yield 3;
    yield 4;
}

foreach (gen() as $val)
{
    echo $val, PHP_EOL;
}

?>
```

以上例程会输出：

```
1
2
3
4
```

## preg\_replace\_callback\_array()

在 PHP 7 之前，当使用 [preg\_replace\_callback()](http://php.net/manual/zh/function.preg-replace-callback.php)

函数的时候， 由于针对每个正则表达式都要执行回调函数，可能导致过多的分支代码。 而使用新加的 [preg\_replace\_callback\_array()](http://php.net/manual/zh/function.preg-replace-callback-array.php)函数， 可以使得代码更加简洁。

现在，可以使用一个关联数组来对每个正则表达式注册回调函数， 正则表达式本身作为关联数组的键， 而对应的回调函数就是关联数组的值。

## CSPRNG Functions

新加入两个跨平台的函数：[random\_bytes()](http://php.net/manual/zh/function.random-bytes.php)和[random\_int()](http://php.net/manual/zh/function.random-int.php)用来产生高安全级别的随机字符串和随机整数。

## 可以使用 list() 函数来展开实现了**ArrayAccess**接口的对象

在之前版本中，[list()](http://php.net/manual/zh/function.list.php)函数不能保证 正确的展开实现了**ArrayAccess**接口的对象， 现在这个问题已经被修复。

## 允许在克隆表达式上访问对象成员

```php
允许在克隆表达式上访问对象成员，例如： (clone $foo)->bar()。
```

## **可以使用关键词作为方法名**

详见：<http://php.net/manual/zh/migration70.other-changes.php>

放宽保留字限制,场景为链式操作时．

全局保留的单词作为属性、常量和方法名称在类、接口和特性中被允许。当引入新的关键字并避免对api的命名限制时，这会减少BC的表面。

这在创建具有连贯接口的内部DSLs时特别有用:

```php
<?php
// 'new', 'private', and 'for' were previously unusable
Project::new('Project Name')->private()->for('purpose here')->with('username here');
?>
```

唯一的限制是类关键字仍然不能用作常量名称，否则它将与类名解析语法(ClassName:class)相冲突。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://php7.shujuwajue.com/php-70x-xin-te-xing/qi-ta-te-xing-he-bian-geng/bu-5145-qi-ta-te-xing-he-bian-geng.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
