Bug #71970 [NEW]: Dynamic class names don't work with aliases
| From: | marcus at synchromedia dot co dot uk | Date: | Tue, 05 Apr 2016 18:05:51 +0000 |
| Subject: | Bug #71970 [NEW]: Dynamic class names don't work with aliases | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-200419@lists.php.net to get a copy of this message | ||
From: marcus at synchromedia dot co dot uk
Operating system:
PHP version: 5.6.20
Package: Class/Object related
Bug Type: Bug
Bug description:Dynamic class names don't work with aliases
Description:
------------
In simple cases, creating a class instance where the name of the class
is in a string, it generally works as per the docs:
$a = 'myclass';
$b = new $a; //works
But if the class name is from an aliased import, it fails.
use Some\Project\myclass;
$a = 'myclass';
$b = new $a; //fails
Adding a __NAMESPACE__ prefix (as per bug #53904) doesn't work because
it uses the current namespace, not the aliased one.
It's impossible to work around this with reflection because the class
may not yet be loaded, so there seems to be no way of getting the
namespace that an aliased name belongs to.
I think this is a bug (or feature) in the order of resolving the class
name before creating it. PHP resolves the class name but does not apply
alias mappings to it before using it.
Test script:
---------------
<?php
namespace my\project;
require 'vendor/autoload.php';
use League\OAuth2\Client\Provider\Google;
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
$provider = 'Google';
$nprovider = __NAMESPACE__ . $provider;
$a = new Google; //Succeeds
$a = new $provider; //Fails
$a = new $nprovider; //Fails - wrong namespace
--
Edit bug report at https://bugs.php.net/bug.php?id=71970&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71970&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71970&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71970&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71970&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71970&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71970&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71970&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71970&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71970&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71970&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71970&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71970&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71970&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71970&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71970&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71970&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71970&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71970&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71970&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71970&r=mysqlcfg