php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68410 new \NumberFormatter does not work in namespace
Submitted: 2014-11-12 12:46 UTC Modified: 2014-11-12 12:52 UTC
From: thomas at nunninger dot info Assigned:
Status: Not a bug Package: PHP Language Specification
PHP Version: 5.6.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
1 + 21 = ?
Subscribe to this entry?

 
 [2014-11-12 12:46 UTC] thomas at nunninger dot info
Description:
------------
If I have a namespace and call

    new \NumberFormatter(...)

inside the namespace I get

    Fatal error: Class 'x\NumberFormatter' not found in /home/thomas/test.php on line 7

If I add

    use \NumberFormatter;

it works.

BTW: I found this error in a class method. The reproducing script does not use classes.

Test script:
---------------
<?php

namespace x;

# use \NumberFormatter;

$nf = new \NumberFormatter( 'de_DE', NumberFormatter::PATTERN_DECIMAL );
var_dump( $nf );


Expected result:
----------------
object(NumberFormatter)#1 (0) {
}


Actual result:
--------------
Fatal error: Class 'x\NumberFormatter' not found in /home/thomas/test.php on line 7


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-12 12:52 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2014-11-12 12:52 UTC] daverandom@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You are getting this error because you did not fully qualify the class name when you accessed the class constant that is being passed as and argument.

The below should resolve the problem:

<?php

namespace x;

$nf = new \NumberFormatter( 'de_DE', \NumberFormatter::PATTERN_DECIMAL );
var_dump( $nf );
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 07:01:27 2024 UTC