在 PHP 代码中使用 UTF8 字符时 get_class_methods 与 method_exists 不一致

发布于 2024-08-22 22:50:24 字数 1837 浏览 1 评论 0原文

我把这个类放在一个名为 EnUTF8.Class.php 的 UTF-8 编码文件中:

class EnUTF8 {

    public function ñññ() {

        return 'ñññ()';

    }
}

和另一个 UTF-8 编码文件中:

require_once('EnUTF8.Class.php');
require_once('OneBuggy.Class.php');

$utf8 = new EnUTF8();
//$buggy = new OneBuggy();

echo (method_exists($utf8, 'ñññ')) ? 'ñññ() exists!' : 'ñññ() does not exist...';

echo "\n\n----------------------------------\n\n"

print_r(get_class_methods($utf8));

echo "\n----------------------------------\n\n"

echo $utf8->ñññ();

产生预期的结果:

ñññ() exists!

----------------------------------

Array
(
    [0] => ñññ
)

----------------------------------

ñññ()

但如果...

require_once('EnUTF8.Class.php');
require_once('OneBuggy.Class.php');

$utf8 = new EnUTF8();
$buggy = new OneBuggy();

echo (method_exists($utf8, 'ñññ')) ? 'ñññ() exists!' : 'ñññ() does not exist...';

echo "\n\n----------------------------------\n\n"

print_r(get_class_methods($utf8));

echo "\n----------------------------------\n\n"

echo $utf8->ñññ();

那么奇怪的事情就会出现! !!:

ñññ() does not exist!

----------------------------------

Array
(
[0] => ñññ
)

----------------------------------

Fatal error: Call to undefined method EnUTF8::ñññ() in /var/www/test.php on line 16

嗯,问题是 OneBuggy.Class.php 也是 UTF-8 编码的,并且与 EnUTF8.Class.php 绝对没有任何共享,所以...

在哪里错误?

更新:

好吧,经过长时间的调试,我在 OneBuggy.Class.php 构造函数中发现了这个:

setlocale (LC_ALL, "es_ES@euro", "es_ES", "esp");

所以我做到了......

//setlocale (LC_ALL, "es_ES@euro", "es_ES", "esp");

现在它可以工作了,但为什么呢?

I have this class in a UTF-8 encoded file called EnUTF8.Class.php:

class EnUTF8 {

    public function ñññ() {

        return 'ñññ()';

    }
}

and in another UTF-8 encoded file:

require_once('EnUTF8.Class.php');
require_once('OneBuggy.Class.php');

$utf8 = new EnUTF8();
//$buggy = new OneBuggy();

echo (method_exists($utf8, 'ñññ')) ? 'ñññ() exists!' : 'ñññ() does not exist...';

echo "\n\n----------------------------------\n\n"

print_r(get_class_methods($utf8));

echo "\n----------------------------------\n\n"

echo $utf8->ñññ();

that produces the expected result:

ñññ() exists!

----------------------------------

Array
(
    [0] => ñññ
)

----------------------------------

ñññ()

but if...

require_once('EnUTF8.Class.php');
require_once('OneBuggy.Class.php');

$utf8 = new EnUTF8();
$buggy = new OneBuggy();

echo (method_exists($utf8, 'ñññ')) ? 'ñññ() exists!' : 'ñññ() does not exist...';

echo "\n\n----------------------------------\n\n"

print_r(get_class_methods($utf8));

echo "\n----------------------------------\n\n"

echo $utf8->ñññ();

then the weirdness appears!!!:

ñññ() does not exist!

----------------------------------

Array
(
[0] => ñññ
)

----------------------------------

Fatal error: Call to undefined method EnUTF8::ñññ() in /var/www/test.php on line 16

Well, the thing is that OneBuggy.Class.php is UTF-8 encoded too and shares absolutly nothing with EnUTF8.Class.php so...

where is the bug?

UPDATED:

Well, after a long debugging time I found this in OneBuggy.Class.php constructor:

setlocale (LC_ALL, "es_ES@euro", "es_ES", "esp");

so I did...

//setlocale (LC_ALL, "es_ES@euro", "es_ES", "esp");

and now it works but why?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-08-29 22:50:24

关于您的更新,我认为它会朝这个方向发展:

使用 setlocale() 等,您可以设置

LC_CTYPE用于字符分类和转换,例如strtoupper()

method_exists()不区分大小写,因此在 method_exists() 中,必须进行一些大小写转换。我敢打赌绳子在那一刻就断了。但我不明白,为什么如果您明确设置西班牙语言环境,它会中断,但如果不设置,则不会中断。

除了 Ñ 之外,对于大写 ñ 是否有特定的西班牙规则? ñ 可以小写吗?

也可能是该函数尝试切换到的西班牙语区域设置根本没有安装在您的系统上,并且后备区域设置与 PHP 默认使用的区域设置不同。

Re your update, I think it goes into this direction:

With setlocale(), among other things, you set

LC_CTYPE for character classification and conversion, for example strtoupper()

method_exists() is case insensitive, so within method_exists(), some case conversion must take place. I bet the string breaks at that point. Why it would break if you explicitly set the spanish locale, but not if you don't, I don't understand, though.

Is there a specific spanish rule for uppercasing ñ other than making it Ñ? Is it possible to lowercase ñ?

It could also be that the spanish locale the function is trying to switch to isn't installed on your system at all, and the fallback locale is a different one than PHP uses by default.

甜柠檬 2024-08-29 22:50:24

如果您使用 PHP 5.x,则不应在变量/类/函数/...中使用 UTF-8 中的名称进行开发:在某些情况下,对于某些字符,它会起作用,但在一般情况下,不会的。

请注意,这对于标识符来说是正确的,但是对于变量的内容,您也会遇到同样的问题,例如,要操作 UTF-8 中的字符串,您必须使用 mb_* 函数系列。

这是因为 PHP 5.x 并没有真正使用 Unicode:它是 PHP 6 计划的重要大事(甚至还没有进入 alpha 阶段)

If you are working with PHP 5.x, you should not develop using names in UTF-8 for your variables/classes/functions/... : in some cases, for some characters, it will work, but in a general situation, it will not.

And note this is true for identifiers, but you'll have the same problem for the content of variables, for instance -- as an example, to manipulate strings in UTF-8, you have to work with the mb_* familly of functions.

This is because PHP 5.x is not really using Unicode : it's the big thing that's planned for PHP 6 (which is not even in alpha stage yet).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文