PHP动态类扩展 [英] PHP dynamic class extending
问题描述
我知道您可以在构造类时扩展类,如下所示:
I know you can extend a class when constructing it like the following:
class b extends a {
}
但是可以从脚本中动态扩展类吗?如:
But is it possible to dynamically extend classes from the scripts? Such as:
$b = new b($input) extends a;
我希望完成的工作是扩展模块,无论是在管理页面中使用的模块,还是在公共页面上使用的模块.我知道我可以用相同的名称创建两个不同的父类,并且每个管理员或公共只包含一个.但是我的问题是,是否有可能在PHP中动态地做到这一点?
What I wish to accomplish is to extend the module differnetly wheither it's used in admin rather than the public pages. I know I can create two different parent classes by the same name and only include one per admin or public. But my question is, is it possible to do it dynamically in PHP?
推荐答案
You can't, but this has been requested for a few years: https://bugs.php.net/bug.php?id=41856&edit=1
您可以在评估中定义类,但这比正常声明类要麻烦得多.
You can define the classes within an eval, but it's more trouble than declaring the class normally.
这篇关于PHP动态类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!