php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61928 Instanciate object for aliased class
Submitted: 2012-05-03 18:53 UTC Modified: 2012-05-07 08:40 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: olav at fwt dot no Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.12 OS: Linux 3.3.4
Private report: No CVE-ID: None
 [2012-05-03 18:53 UTC] olav at fwt dot no
Description:
------------
Creating a class from variable when it has been imported with use keyword fails 
with class not found error.

This problems persists in 5.3.11, 5.3.12 and 5.4.1


Test script:
---------------
use stdClass as Foo;

$class = "Foo";
new $class;


Expected result:
----------------
No output

Actual result:
--------------
PHP Fatal error:  Class 'Foo' not found in /tmp/test.php on line 6
PHP Stack trace:
PHP   1. {main}() /tmp/test.php:0

Fatal error: Class 'Foo' not found in /tmp/test.php on line 6

Call Stack:
    0.0003     631856   1. {main}() /tmp/test.php:0


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-03 19:01 UTC] olav at fwt dot no
It seems this problem is not related to the use keyword but imported 
items in general:

file: test2.php
------------------------------------
<?php
namespace Foo;

class Bar { }


file: test.php
------------------------------------
<?php
namespace NS2;

include 'test1.php';

use Foo\Bar;

$class = "Bar";
new $class;


Results is the same:
---------------------------
Fatal error: Class 'Bar' not found in /tmp/test.php on line 9

Call Stack:
    0.0003     632520   1. {main}() /tmp/test.php:0
 [2012-05-07 08:40 UTC] maarten@php.net
-Status: Open +Status: Not a bug
 [2012-05-07 08:40 UTC] maarten@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This isn't a bug, see Example 3 @ http://www.php.net/manual/en/language.namespaces.importing.php and the note "Importing is performed at compile-time, and so does not affect dynamic class, function or constant names."

So your code is referencing Foo in the root namespace, which is the documented behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 14:01:32 2024 UTC