douyingbei1458 2010-08-09 20:03
浏览 9
已采纳

避免在一般的“bootstrap”文件中包含抽象类

In my project, say I am instantiating a class named SubClass, which extends an abstract class named AbstractClass. At the top of my files I include a file named bootstrap.inc, which includes AbstractClass in every file. Then, when I need it, I include SubClass which does not contain an include to AbstractClass.

If I include AbstractClass in the files of classes that extend it (such as SubClass), sometimes I am using multiple sub-classes in one file and will get an error. I would rather not include AbstractClass in every file via the bootstrap, since I am not always using it, but cannot think of a way to avoid this.

I have been advised that using include_once() is a sign of poor design, so have avoided that, although it would be one solution.

Is there an alternative method to include_once()?

  • 写回答

2条回答 默认 最新

  • dongxiatuo9827 2010-08-09 20:14
    关注

    First off - it's generally a good idea to stick to one class per file. If you're worried about performance, you're being paranoid. In production, you can always turn on APC with stat off.

    Secondly - there's nothing wrong with include_once/require_once.

    Unless you're going to set up some fancy autoloading magic, just stick to one class per file.

    Absent some specific concern (that is, something articulable that you actually understand, not just some rumor you heard), the following pattern is perfectly fine:

    <?PHP
    require_once 'MyAbstract.class.php';
    
    class Concrete extends MyAbstract {
     //...
    }
    // end-of-file
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大