dqvrlgi3247 2010-02-19 11:05
浏览 54
已采纳

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

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 "

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

"

print_r(get_class_methods($utf8));

echo "
----------------------------------

"

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 "

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

"

print_r(get_class_methods($utf8));

echo "
----------------------------------

"

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?.

  • 写回答

2条回答 默认 最新

  • douyingtai6662 2010-02-19 11:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)