You can't do this easily as of PHP 5.3-5.5.
There are only a handful of ways to determine what the "current" class is when a call is made. All of them return the unaliased class.
class First {
public function test1() { echo get_called_class(); }
public function test2() { print_r(debug_backtrace()); }
public function test3() { echo get_class($this); }
public function test4() { echo __CLASS__; }
public static function test5() { echo get_called_class(); }
}
class_alias('First', 'Second');
$aliased = new Second();
$aliased->test1(); // First
$aliased->test2(); // array( 0 => array( ... [object] => First Object, ... ) )
$aliased->test3(); // First
$aliased->test4(); // First
Second::test5(); // First
3v4l demo.
This is because class_alias
doesn't create a new class with the new name, it creates another entry in the list of classes that points at the same class as the original. When you ask PHP to look up what class is being used, it finds the original class, not the alias.
If you need to create a new class is nothing more the original class with a different name, you'll need to do so via inheritance. You can do this dynamically using eval
. I can't believe I'm going to recommend eval
for something. Eww.
class First {
public function test1() { echo get_called_class(); }
public function test2() { print_r(debug_backtrace()); }
public function test3() { echo get_class($this); }
public function test4() { echo __CLASS__; }
public static function test5() { echo get_called_class(); }
}
function class_alias_kinda($original, $alias) {
eval("class $alias extends $original {}");
}
class_alias_kinda('First', 'Second');
$aliased = new Second();
$aliased->test1(); // Second
$aliased->test2(); // array( 0 => array( ... [object] => Second Object, ... ) )
$aliased->test3(); // Second
$aliased->test4(); // First (this is expected)
Second::test5(); // Second
3v4l demo.
This might not work well for all cases. In PHP, private members can not be seen by descendant classes, so this might break your code horribly.