CakeFest 2024: The Official CakePHP Conference

函数重载功能

警告

本特性自 PHP 7.2.0 起废弃,并且自 PHP 8.0.0 起被移除。 强烈建议不要使用本特性。

你也许常常会发现现存的 PHP 应用很难运行在多字节环境下。 发生这种情况的原因是大多数那种 PHP 应用使用了标准的字符串函数,类似 substr(),已知无法处理多字节编码的字符串。

mbstring 支持一个“函数重载”功能,将对应的多字节版本重载到标准字符处理函数上,例如你能够让这类应用在不修改代码的前提下添加多字节的处理能力。 比如,启用函数重载后,mb_substr() 将会代替 substr() 被调用。 在很多情况下这个功能允许让仅支持单字节编码的应用简单地和多字节环境对接。

要使用函数重载功能,设置 php.ini 里的 mbstring.func_overload 为正值,就是表示为重载函数分类的位掩码组合。 要重载 mail() 函数需要设置它为 1。字符串函数设置为 2,正则表达式函数为 4。 例如,当它设置为 7, mail、strings 和 正则表达式函数将都会被重载。 以下列表显示了重载的函数。

被重载的函数
mbstring.func_overload 的值 原始函数 重载后的函数
1 mail() mb_send_mail()
2 strlen() mb_strlen()
2 strpos() mb_strpos()
2 strrpos() mb_strrpos()
2 substr() mb_substr()
2 strtolower() mb_strtolower()
2 strtoupper() mb_strtoupper()
2 stripos() mb_stripos()
2 strripos() mb_strripos()
2 strstr() mb_strstr()
2 stristr() mb_stristr()
2 strrchr() mb_strrchr()
2 substr_count() mb_substr_count()

注意:

不推荐每个目录的范围(context)内使用函数重载选项,因为还无法确定在生产环境中是否稳定,也许会导致不确定的行为。

add a note

User Contributed Notes 4 notes

up
52
Anonymous
8 years ago
it's kinda evil and gives you tons of headache.
up
38
Anonymous
7 years ago
I took over a project with this "feature" enabled... took me a while to figure out why strings behaved non language standard. So, if you like to make grown men cry - by all means enable this deathtrap.
up
17
Grandt
9 years ago
In short, only use mbstring.func_overload if you are 100% certain that nothing on your site relies on manipulating binary data in PHP.
up
-2
Oliver Baltz
13 years ago
In case you need to (de)activate the overloading for a specific directory, try setting an appropriate php_admin_value in your httpd.conf, e.g.

<Directory ...>
...
php_admin_value mbstring.func_overload 7
</Directory>

I'm not 100% sure if one can rely on that, but it seems to work for me.
To Top