From: Operating system: Mac OS X, possibly others PHP version: 5.3.6 Package: Scripting Engine problem Bug Type: Bug Bug description:Hard-coded class creation doesn't follow same rules as dynamic (string-based)
Description: ------------ Using PHP 5.3.6 on Mac OS X, I've found that hard-coding a class name into a script works fine, but attempting to create the same class using a string will not work, when relative namespaces are involved. Test script: --------------- <?php namespace First\Second { class Test { } } namespace First { try { // This will work, as we're in First, so Second is relative. echo 'Creating hard-coded instance...', PHP_EOL; $instance = new Second\Test; echo 'Done.', PHP_EOL; } catch (Exception $e) { echo 'Failed!', PHP_EOL; } try { // This will *not* work, you have to use an absolute namespace like 'First\\Second\\Test'. echo 'Creating instance from string...', PHP_EOL; $class = 'Second\\Test'; $instance = new $class; echo 'Done.', PHP_EOL; } catch (Exception $e) { echo 'Failed!', PHP_EOL; } } Expected result: ---------------- I'd expect the same results from either a string or a hard-coded class name. Actual result: -------------- PHP Fatal error: Class 'Second\Test' not found -- Edit bug report at https://bugs.php.net/bug.php?id=55170&edit=1 -- Try a snapshot (PHP 5.2): https://bugs.php.net/fix.php?id=55170&r=trysnapshot52 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55170&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55170&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=55170&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55170&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=55170&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=55170&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=55170&r=needscript Try newer version: https://bugs.php.net/fix.php?id=55170&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=55170&r=support Expected behavior: https://bugs.php.net/fix.php?id=55170&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=55170&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=55170&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=55170&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55170&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=55170&r=dst IIS Stability: https://bugs.php.net/fix.php?id=55170&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=55170&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=55170&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=55170&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=55170&r=mysqlcfg Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55170&r=trysnapshot54