Edit report at https://bugs.php.net/bug.php?id=55170&edit=1
ID: 55170
Comment by: phpbug at catchall dot drarok dot com
Reported by: phpbug at catchall dot drarok dot com
Summary: Hard-coded class creation doesn't follow same rules
as dynamic (string-based)
Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Mac OS X, possibly others
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
Well, if this isn't a bug, then surely it's at least an undocumented "feature"?
Should it be mentioned somewhere? Or was that response a knee-jerk reaction?
Dynamic use of classes is pretty widely used, after all.
Previous Comments:
------------------------------------------------------------------------
[2011-07-09 21:49:01] [email protected]
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2011-07-09 21:29:21] phpbug at catchall dot drarok dot com
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 this bug report at https://bugs.php.net/bug.php?id=55170&edit=1