du2229 2009-11-18 07:17
浏览 43

如何让15个静态PHP类很好地协同工作?

I have a personal PHP application that has about 15 classes. Each class is only initiated once as a page is executed. In other words, when the page loads:

  • 15 classes are loaded, and as each class file gets loaded, I create one instance of the class.
  • The application (so far) is designed so every variable in the system has one state during the generation of a page. I use global vars to access each of these
  • It's worked fine for 3 years, but I am the sole developer and a goo debugger of my own code.

I have heard all the issues with Singletons, and I hate doing, "global $var" all over the place. Please tell me how to pull this type of structure out and into something developers would love. I want to write software the right way, but I can't seem to find a very simple code framework for this type of execution.

Oh - and I'm not looking for a MVC framework solution. I would love your thoughts on how I take 15 classes and turn them into a proper framework for working together. I would also love an articulation on how "stupid" it is to develop this way.

  • 写回答

3条回答 默认 最新

  • drtkyykai004574380 2009-11-18 07:30
    关注

    If you have variables you want all classes to have access to, maybe you could try inheritance, for example something like

    class Settings
    {
        var $page_name = "My Page";
        var $database_name = "my_db";
    }
    

    and then let all classes inherit this class like

    class Page extends Settings
    {
        var $id = 0;
        var $template = "";
        function __construct() {}
    }
    
    $page = new Page();
    echo $page->page_name;
    

    or you could define the values as constants, if that is what they are

    define("PAGE_NAME", "My Page");
    

    you would have access to PAGE_NAME everywhere

    I'm not in any way saying this is the right way, it's just a way. :)

    评论

报告相同问题?

悬赏问题

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