在PHP中,使用class_alias时如何获得被调用的别名类? [英] In PHP, how do you get the called aliased class when using class_alias?

查看:255
本文介绍了在PHP中,使用class_alias时如何获得被调用的别名类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为其他类名称设置类别名的类.通过别名类在此类内部调用函数时,我需要知道使用了哪个别名.有没有办法在PHP中做到这一点?

I have a class that sets up a class alias for other class names. When a function is called inside of this class via the aliased class, I need to know which alias was used. Is there a way to do this in PHP?

我尝试了以下代码:

class foo
{
  public static function test()
  {
    var_dump(get_called_class());
  }
}

class_alias('foo', 'bar');

foo::test();
bar::test();

哪个输出:

string 'foo' (length=3)
string 'foo' (length=3)

但是我希望bar::test();代替输出string 'bar' (length=3).抓住吸管,__CLASS__get_class()都产生相同的结果.我似乎在PHP文档中找不到其他任何可以帮助解决此问题的东西,但希望我能忽略一些事情.

But I want bar::test(); to output string 'bar' (length=3) instead. Grasping at straws, __CLASS__ and get_class() all produce the same result. I can't seem to find anything else in the PHP documentation that would help me with this problem, but hopefully I am overlooking something.

使用class_alias时如何获得被称为 aliased 的类?

How do you get the called aliased class when using class_alias?

推荐答案

从PHP 5.3-5.5开始,您不能轻易做到这一点.

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演示.

这是因为class_alias不会使用新名称创建 new 类,它会在类列表中创建另一个条目,该条目指向与原始类相同的类.当您要求PHP查找正在使用的类时,它将找到原始类,而不是别名.

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.

如果您需要创建一个新类,而仅仅是具有不同名称的原始类,则需要通过继承来实现.您可以使用eval动态地执行此操作.我不敢相信我会推荐eval.哇!

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演示.

这可能不适用于所有情况.在PHP中,后代类不能 看到私有成员,因此这可能会严重破坏您的代码.

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.

这篇关于在PHP中,使用class_alias时如何获得被调用的别名类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆