php.net |  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: 2010-10-24 15:37 UTC
Votes:2
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:
Status: Open 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
Description:
------------
Cant create alias to class in different namespace.
Created object (alias instance) return type of base class,
not alias.

Test script:
---------------
<?php

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:
----------------
tf\users\test

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-13 00:34 UTC] felipe@php.net
-Status: Open +Status: Assigned -Package: Class/Object related +Package: Scripting Engine problem -Assigned To: +Assigned To: dmitry
 [2010-06-13 00:34 UTC] felipe@php.net
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');
ReflectionClass::export('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] dmitry@php.net
-Status: Assigned +Status: Open -Package: Scripting Engine problem +Package: Documentation problem -Assigned To: dmitry +Assigned To:
 [2010-06-15 09:48 UTC] dmitry@php.net
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?

is_alias()
get_alias_name()
or reflection method?

in fact we cant get alias name, aliasing is useless.
 [2010-10-23 06:46 UTC] kalle@php.net
-Type: Bug +Type: Documentation Problem
 [2010-10-24 15:36 UTC] kalle@php.net
-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] kalle@php.net
Moving this to a feature / change request
 [2010-10-24 15:37 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: dmitry +Assigned To:
 [2010-10-24 15:37 UTC] kalle@php.net
Sorry, shouldn't have been assigned to you Dmitry ;-)
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Aug 24 20:01:26 2019 UTC