|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-07-09 21:49 UTC] cataphract@php.net
-Status: Open
+Status: Bogus
[2011-07-09 21:49 UTC] cataphract@php.net
[2011-07-09 22:04 UTC] phpbug at catchall dot drarok dot com
[2015-06-18 18:01 UTC] cmb@php.net
-Status: Not a bug
+Status: Duplicate
[2015-06-18 18:01 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 22:00:01 2025 UTC |
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