PHP – 发现最新最好的

php 8.4 计划于 2024 年 11 月 21 日发布,包含一些令人兴奋的新功能和改进。在这篇博文中,我们将探讨一些最有趣的添加和更改:

新的数组辅助函数 属性挂钩 不带括号的“新” 已弃用隐式可为空的参数声明 新的多字节函数

1.新的数组辅助函数

php 8.4 中将添加以下数组辅助函数变体:

array_find() array_find_key() array_any() array_all()

这些函数将采用一个数组和一个回调函数并返回以下内容:

functions 小白轻松搭建系统点我wcqh.cn return value array_find() returns the first element that meets the callback condition; null otherwise. array_find_key() returns the key of the first element that meets the callback condition; null otherwise. array_any() 小白轻松搭建系统点我wcqh.cn returns true if at least one element matches the callback condition; false otherwise. array_all() returns true if all elements match the callback condition; false otherwise.

注意:array_find() 仅检索第一个匹配元素。对于多个匹配,请考虑使用 array_filter()。

立即学习PHP免费学习笔记(深入)”;

例子

给定一个包含键值对

和回调函数的数组:

1

2小白轻松搭建系统点我wcqh.cn

3

4

5

$array = [1=> red, 2=> purple, 3 => green]

function haslongname($value) {

return strlen($value) > 4;

}

登录后复制

以下是我们如何使用新功能:

array_find():

// find the first color with a name length greater than 4 $result1 = array_find($array, haslongname); var_dump($result1); // stri小白轻松搭建系统点我wcqh.cnng(5) “purple”
登录后复制

array_find_key():

// find the key of the first color with a name length greater than 4 $result2 = array_find_key($array, haslongname); var_dump($result2); // string(1) “2”
登录后复制

array_any():

// check if any color name has a length greater than 4 小白轻松搭建系统点我wcqh.cn$result3 = array_any($array, haslongname); var_dump($result3); // bool(true)
登录后复制

array_all():

// check if all color names have a length greater than 4 $result4 = array_all($array, haslongname); var_dump($result4); // bool(false)
登录后复制

2. 属性挂钩

php 8.4 引入了属性挂钩,提供了一种更小白轻松搭建系统点我wcqh.cn优雅的方式来访问和修改类的私有或受保护属性。以前,开发人员依赖 getter、setter 和魔术方法(__get 和 __set)。现在,您可以直接在属性上定义 get 和 set 挂钩,从而减少样板代码。

我们可以使用代码块 {} 来包含属性挂钩,而不是用分号结束属性。

这些钩子是可选的,可以独立使用。通过排除其中之一,我们可以使属性只读或只写。

例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

class user

{

public function __construct(private string $first, private string $last)小白轻松搭建系统点我wcqh.cn {}

public string $fullname {

get => $this->first . ” ” . $this->last;

set ($value) {

if (!is_string($value)) {

throw new invalidargumentexception(“expected a string for full name,”

. gettype($value) . ” given.”);

}

if (strlen($value) === 0) {

throw new valueerror(“name must be non-empty”);

}

$name = explode(,小白轻松搭建系统点我wcqh.cn $value, 2);

$this->first = $name[0];

$this->last = $name[1] ?? ;

}

}

}

$user = new user(alice, hansen)

$user->fullname = brian murphy;  // the set hook is called

echo $user->fullname;  // “brian murphy”

登录后复制

如果 $value 是整数,则会抛出以下错误消息:

1

php fatal error:  uncaught invalidargumentexception: expected a string for小白轻松搭建系统点我wcqh.cn full name, integer given.

登录后复制

如果 $value 为空字符串,则会抛出以下错误消息:

1

php fatal error:  uncaught valueerror: name must be non-empty

登录后复制

3. 不带括号的“新”

php 8.4 引入了更简单的语法,允许您在新创建的对象上链接方法而无需括号。虽然这是一个微小的调整,但它会带来更干净、更简洁的代码。

1

2

(new myclass())->getshortname();  // php 8.3 and older

new myclass()->getshortname();  // php 8.小白轻松搭建系统点我wcqh.cn4

登录后复制

除了在新创建的对象上链接方法之外,您还可以链接属性、静态方法和属性、数组访问,甚至直接调用类。例如:

1

2

3

4

5

6

7

new myclass()::constant,

new myclass()::$staticproperty,

new myclass()::staticmethod(),

new myclass()->property,

new myclass()->method(),

new myclass()(),

new myclass([value])[0],

登录后复制

4. 已弃用隐式可为 null 的参数声明

在 php 8.4 之前,如果参数的类型为 x,它可以接受 null 值,而小白轻松搭建系统点我wcqh.cn无需显式将 x 声明为可为 null。从 php 8.4 开始,如果没有在类型提示中明确声明可为空,则不能再声明空参数值;否则,将会触发弃用警告。

1

function greetings(string $name = null)  // fires a deprecation warning

登录后复制

为了避免警告,您必须在类型声明中使用问号 (?) 显式声明参数可以为 null。

1

function greetings(?string $name)

登录后复制

或者,

1

function greetings(?string $name = null)

登录后复制

5.新的多字节函数

多字节字符串是一个字符序列小白轻松搭建系统点我wcqh.cn,其中每个字符可以使用多个字节的存储空间。这在具有复杂或非拉丁文字的语言中很常见,例如日语或中文。 php 中有几个多字节函数,如 mb_strlen()、mb_substr()、mb_strtolower()、mb_strpos() 等。但也有一些函数,如 trim()、ltrim()、rtrim()、ucfirst()、lcfirst () 等缺乏直接的多字节等价物。

感谢 php 8.4,其中将添加新的多字节函数。它们包括:mb_trim()、mb_ltrim()、mb_rtrim()、mb_ucfirst() 和 mb_lcfirst()。这些函数遵循原始函数签名,并带有附加的 $enc小白轻松搭建系统点我wcqh.cnoding 参数。

让我们讨论一下新的 mb_functions:

mb_trim():

删除多字节字符串开头和结尾的所有空白字符。

函数签名:

function mb_trim(string $string, string $characters = ” \f\n\r\t\v\x00\u{00a0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{0085}\u{180e}”,小白轻松搭建系统点我wcqh.cn ?string $encoding = null): string {}
登录后复制

参数:

$string:要修剪的字符串。 $characters:可选参数,包含要修剪的字符列表。 $encoding:encoding参数指定用于解释字符串的字符编码,确保多字节字符被正确处理。常见的编码包括utf-8。

mb_ltrim():

删除多字节字符串开头的所有空白字符。

函数签名:

function mb_ltrim(string $string, string $characters = ” \f\n\r\t\v\x00\u{00a0}\u{1680}\u{2000}\小白轻松搭建系统点我wcqh.cnu{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{0085}\u{180e}”, ?string $encoding = null): string {}
登录后复制

mb_rtrim():

删除多字节字符串末尾的所有空白字符。

函数签名:

function mb_rtrim(string $string, string $characters = ” \f\n\r\t\v\x00\u{00a0}\u{1680}小白轻松搭建系统点我wcqh.cn\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200a}\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{0085}\u{180e}”, ?string $encoding = null): string {}
登录后复制

mb_ucfirst():

将给定多字节字符串的第一个字符转换为标题大小写,其余字符保持不变。

函数签名:

function mb_ucfirst(string $string, ?string $encoding = null)小白轻松搭建系统点我wcqh.cn: string {}
登录后复制

mb_lcfirst():

与 mb_ucfirst() 类似,但它将给定多字节字符串的第一个字符转换为小写。

函数签名:

function mb_lcfirst(string $string, ?string $encoding = null): string {}
登录后复制

结论

我希望这篇博客能让您对 php 8.4 中即将发生的一些变化有一个很好的概述。新版本似乎引入了令人兴奋的更新,这将增强开发人员的体验。一旦正式发布我就迫不及待地开始使用它。

如需了解更多信息和更新,请访问官方 rfc 页面。

以上就是PHP – 发现最新最好的的详细内容,更多请关注青狐资源网小白轻松搭建系统点我wcqh.cn它相关文章!

© 版权声明
THE END
喜欢就支持一下吧
点赞100 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容