|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52029 create_alias namespaces
Submitted: 2010-06-09 13:08 UTC Modified: 2021-06-02 14:58 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: rustyj4ck at gmail dot com Assigned: cmb (profile)
Status: Wont fix Package: Class/Object related
PHP Version: 5.3.2 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-06-09 13:08 UTC] rustyj4ck at gmail dot com
Cant create alias to class in different namespace.
Created object (alias instance) return type of base class,
not alias.

Test script:

namespace tf\users {}

namespace tf\core {
    class collection {
        private $_cl;
        function __construct()  { $this->_cl = get_class($this);  }        
        function get_cl()       { return $this->_cl; }
    class core {
        private $_cl;
        function register($name) {
            $from = 'tf\\core\\collection';
            $to =   'tf\\users\\' . $name;
            printf ("aliasing %s --> %s : %s <br/> \n",  $from, $to, class_alias($from, $to)?'OK':'FAIl');
            $a = new $to ();
            $this->_cl = get_class($a);
            return $a;
        function get_cl()       { return $this->_cl; }

namespace {
    use tf\core\core;
    $core = new core();
    $test = $core->register('test');
    printf("core: %s, result: %s, get_class: %s <br/>\n", $core->get_cl(), $test->get_cl(), get_class($test));

Expected result:

Actual result:
aliasing tf\core\collection --> tf\users\test : OK 
core: tf\core\collection, result: tf\core\collection, get_class: tf\core\collection 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-13 00:34 UTC]
-Status: Open +Status: Assigned -Package: Class/Object related +Package: Scripting Engine problem -Assigned To: +Assigned To: dmitry
 [2010-06-13 00:34 UTC]
Hmm, in fact the alias is created by reusing the original class entry. I.e. no copy is done, it just add a new entry to the class table using the alias name pointing to the original one.

class a { }
class_alias('a', 'b');

Class [ <user> class a ] {

Dmitry, are actually the aliases supposed to behave in this way? (needing to be explained in the documentation wether it is right as is)
 [2010-06-15 09:48 UTC]
-Status: Assigned +Status: Open -Package: Scripting Engine problem +Package: Documentation problem -Assigned To: dmitry +Assigned To:
 [2010-06-15 09:48 UTC]
This is an expected behaviour. An alias allows to refer to a class entry using different name but it's the same class entry.
 [2010-06-15 12:41 UTC] rustyj4ck at gmail dot com
Then, where is expected functions?

or reflection method?

in fact we cant get alias name, aliasing is useless.
 [2010-10-23 06:46 UTC]
-Type: Bug +Type: Documentation Problem
 [2010-10-24 15:36 UTC]
-Status: Open +Status: Assigned -Type: Documentation Problem +Type: Feature/Change Request -Package: Documentation problem +Package: Class/Object related -Assigned To: +Assigned To: dmitry
 [2010-10-24 15:36 UTC]
Moving this to a feature / change request
 [2010-10-24 15:37 UTC]
-Status: Assigned +Status: Open -Assigned To: dmitry +Assigned To:
 [2010-10-24 15:37 UTC]
Sorry, shouldn't have been assigned to you Dmitry ;-)
 [2021-06-02 14:58 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-06-02 14:58 UTC]
It seems to me that class_alias() has been introduced to provide a
way forward to use namespaced classes without breaking BC for
clients.  This is now obsolete, so I don't think that adding
further support makes sense at this point in time.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Oct 24 07:03:34 2021 UTC