使用XAMPP和PHP 5.4.4的Normalizer致命错误

When I use the PHP class Normalizer (e.g. \Normalizer::normalize($string, Normalizer::FORM_KD);) in my class Text.php in custom namespace Utils, I get the following error:

Fatal error: Class 'Utils\Normalizer' not found 
in C:\xampp\htdocs\MyProject\src\Utils\text.php on line 380

My project runs under XAMPP with PHP 5.4.4. I know also that:

  • Normalizer should work since PHP 5 >= 5.3.0.
  • The extension php_intl.dll is enabled in my php.ini file.

What am I missing?

You forgot the leading backslash In Normalizer::FORM_KD when calling \Normalizer::normalize($string, Normalizer::FORM_KD) notice you only used one backslash in front of the class name, but not when you're using the class constant.

In PHP when you declare or import a namespace you are telling PHP to alias all of your function, class/interface, and constant names to your namespace.

So the following code would issue a similar fatal error...

<?php
namespace foo;

var_dump(DateTime::createFromFormat('Y-m-d','2012-12-06'));

The above code would issue an error of PHP Fatal error: Class 'foo\DateTime' not found in /testfile.php on line 4

To fix this make sure you always call those functions/classes/interfaces/constants which are not declared in your namespace/alias to the global namespace.

<?php
namespace foo;
/* Notice the leading backslash infront of DateTime*/
var_dump(\DateTime::createFromFormat('Y-m-d','2012-12-06'));