|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 22:00:01 2025 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'); 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)