douyan1970 2011-05-01 15:27
浏览 52
已采纳

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

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
}
  • 写回答

1条回答 默认 最新

  • dongyigua4468 2011-05-01 15:34
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line