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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题