PHP get_called_class()替代方案 [英] PHP get_called_class() alternative

查看:468
本文介绍了PHP get_called_class()替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象PHP超类,其中包含需要知道其运行的子类的代码。

I've got an Abstract PHP superclass, which contains code that needs to know which subclass its running under.

class Foo {
    static function _get_class_name() {
        return get_called_class();
        //works in PHP 5.3.*, but not in PHP 5.2.*
    }

    static function other_code() {
        //needs to know
        echo self::_get_class_name();
    }
}

class Bar extends Foo {
}

class FooBar extends Foo {
}

Bar::other_code(); // i need 'Bar'
FooBar::other_code(); // i need 'FooBar'

如果我调用函数 get_called_class() - 但是,此代码将在PHP 5.2版中运行。*,因此该函数不可用。

This would work if I called the function get_called_class() -- however, this code is going to be run in PHP version 5.2.*, so that function is not available.

那里有一些 get_called_class()的自定义PHP实现,但它们都依赖于通过 debug_backtrack(),解析文件名&行号,并运行正则表达式(因为编码器不知道PHP 5.2有反射)来查找类名。这段代码需要能够用php运行,即。不仅来自.php文件。 (它需要在 php -a shell或 eval()语句中工作。)

There's some custom PHP implementations of get_called_class() out there, but they all rely on going thru the debug_backtrack(), parsing a file name & line number, and running a regex (as the coder is not aware that PHP 5.2 has reflection) to find the class name. This code needs to be able to be run with php, ie. not only from a .php file. (It needs to work from a php -a shell, or an eval() statement.)

理想情况下,解决方案可以在不需要将任何代码添加到子类的情况下工作......我能看到的唯一可能的解决方案是将以下代码添加到每个子类中,这显然是令人作呕的hack:

Ideally, a solution would work without requiring any code to be added to the subclasses… The only potential solution I can see though is adding the following code to each subclass, which is obviously a disgusting hack:

class FooBar extends Foo {
    static function _get_class_name() {
        return 'FooBar';
    }
}

编辑:等等,这似乎甚至没有工作。这将是我的最后一招。任何人都可以想到类似于这个解决方案的东西,它可以让我获得所需的功能。也就是说,我愿意接受一个解决方案,它要求我为每个子类添加一个函数或变量,告诉它它的类名是什么。不幸的是,似乎从超类调用 self :: _ get_class_name()会调用父类的实现,即使子类已经覆盖了它。

Wait, this doesn't even seem to work. It would've been my last resort. Can anybody think of something similar to this solution that'd get me the required functionality. Ie., I'm willing to accept a solution that requires me to add one function or variable to each subclass telling it what its class name is. Unfortunately, it seems that calling self::_get_class_name() from the superclass calls the parent class' implementation, even if the subclass has overridden it.

推荐答案

实际上,在执行超类方法时,知道实际的被调用(子)类有助于经常,我不同意想要解决这个问题有什么不对。

In reality it is often helpful to know the actual called (sub)class when executing a superclass method, and I disagree that there's anything wrong with wanting to solve this problem.

示例,我的对象需要知道类名,但是他们对这些信息的处理方式总是相同的,可以提取到超类方法 IF 我能够获得被叫类名。甚至PHP团队也认为这非常有用,可以包含在php 5.3中。

Example, my objects need to know the class name, but what they do with that information is always the same and could be extracted into a superclass method IF I was able to get the called class name. Even the PHP team thought this was useful enough to include in php 5.3.

据我所知,正确和非预告的答案是在5.3之前,你必须做一些令人发指的事情(例如回溯),或者只是在每个子类中包含重复的代码。

The correct and un-preachy answer, as far as I can tell, is that prior to 5.3, you have to either do something heinous (e.g. backtrace,) or just include duplicate code in each of the subclasses.

这篇关于PHP get_called_class()替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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