简体   繁体   English

可以动态设置 class 的名称吗?

[英]Possible to dynamically set the name of a class?

I have a mediawiki and phpbb install that I integrated single sign on with successfully.我有一个 mediawiki 和 phpbb 安装,我成功地集成了单点登录。

The problem was that they both had a user class, so in mediawiki, when I called on phpbb it gave a class redecoration error.问题是他们都有一个用户 class,所以在 mediawiki 中,当我调用 phpbb 时,它给出了 class 重新装修错误。

I got around this by checking if the file is being loaded by mediawiki.我通过检查 mediawiki 是否正在加载文件来解决这个问题。 If it is, it called a complete copy of the user class called phpbb_user instead.如果是,则调用名为 phpbb_user 的用户 class 的完整副本。 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?有没有更好的方法来做到这一点,不需要用户 class 的 2 个副本?

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
}

This is a bit of a hack, but you could have a base class, where everything if defined.这有点小技巧,但你可以有一个基础 class,如果定义了所有内容。 Then you simply extend empty subclasses from that class, thus changing its name without having to keep duplicate code.然后,您只需从该 class 扩展空子类,从而更改其名称而无需保留重复代码。

class __user extends session {
    // user class code
}

if (!defined('FROM_MEDIAWIKI')){
    class User extends __user {}
}else{
    class phpbb_user extends __user {}
}

I'd love to see a better solution, though.不过,我希望看到更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM