可以动态设置类的名称吗?
我有一个 mediawiki 和 phpbb 安装,我成功地集成了单点登录。
问题是它们都有一个用户类,所以在 mediawiki 中,当我调用 phpbb 时,它给出了类重新装饰错误。
我通过检查 mediawiki 是否正在加载文件来解决这个问题。如果是,它会调用名为 phpbb_user 的用户类的完整副本。但我想知道是否有更好的方法。这就是它目前的工作方式
if (!defined('FROM_MEDIAWIKI')){
class User extends session{
//user class code
}
}else{
class phpbb_user extends session{
//exact copy of user class code
}
}
是否有更好的方法来做到这一点,不需要用户类的 2 个副本?
我知道你不能这样做,但你能做类似的事情吗?
$className = (defined('FROM_MEDIAWIKI'))? 'phpbb_user' : 'User';
class $className extends session{
//user class code
}
I have a mediawiki and phpbb install that I integrated single sign on with successfully.
The problem was that they both had a user class, so in mediawiki, when I called on phpbb it gave a class redecoration error.
I got around this by checking if the file is being loaded by mediawiki. If it is, it called a complete copy of the user class called phpbb_user instead. But I'm wondering if there is a better way. This is how it currently works
if (!defined('FROM_MEDIAWIKI')){
class User extends session{
//user class code
}
}else{
class phpbb_user extends session{
//exact copy of user class code
}
}
Is there a better way to do this that does not require 2 copies of the user class?
I know you can not do this but can you do something like it?
$className = (defined('FROM_MEDIAWIKI'))? 'phpbb_user' : 'User';
class $className extends session{
//user class code
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点麻烦,但是您可以有一个基类,其中定义了所有内容。然后,您只需从该类扩展空子类,从而更改其名称,而不必保留重复的代码。
不过,我很想看到更好的解决方案。
This is a bit of a hack, but you could have a base class, where everything if defined. Then you simply extend empty subclasses from that class, thus changing its name without having to keep duplicate code.
I'd love to see a better solution, though.