Bug #55170 [Nab->Dup]: Hard-coded class creation doesn't follow same rules as dynamic (string-based)
| From: | cmb@php.net | Date: | Thu, 18 Jun 2015 18:01:44 +0000 |
| Subject: | Bug #55170 [Nab->Dup]: Hard-coded class creation doesn't follow same rules as dynamic (string-based) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193666@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55170&edit=1
ID: 55170
Updated by: cmb@php.net
Reported by: phpbug at catchall dot drarok dot com
Summary: Hard-coded class creation doesn't follow same rules
as dynamic (string-based)
-Status: Not a bug
+Status: Duplicate
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:
Actually, this is a duplicate of bug #45197 and bug #46310, where the reason for this behavior is
explained.
Previous Comments:
------------------------------------------------------------------------
[2011-07-09 22:04:06] phpbug at catchall dot drarok dot com
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.
------------------------------------------------------------------------
[2011-07-09 21:49:01] cataphract@php.net
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