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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thomas at nunninger dot info
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sun Dec 22 06:01:30 2024 UTC