Bug #71970 [Opn->Nab]: Dynamic class names don't work with aliases

From: Date: Thu, 07 Apr 2016 06:21:45 +0000
Subject: Bug #71970 [Opn->Nab]: Dynamic class names don't work with aliases
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200387@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71970&edit=1 ID: 71970 Updated by: nikic@php.net Reported by: marcus at synchromedia dot co dot uk Summary: Dynamic class names don't work with aliases -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related PHP Version: 5.6.20 Block user comment: N Private report: N New Comment: String class names always expect a fully qualified name. If you want to get the fully qualified name of an aliased, use myclass::class notation. Previous Comments: ------------------------------------------------------------------------ [2016-04-05 18:05:47] marcus at synchromedia dot co dot uk 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 this bug report at https://bugs.php.net/bug.php?id=71970&edit=1

« previous php.bugs (#200387) next »