douyinghuo8874 2009-04-22 20:38
浏览 47
已采纳

OOP如何设法“包含”存储在不同文件中的类[关闭]

I'm trying to move into OOP development and am turning to here as I'm sick of searching the web and finding only the very basic information about what classes are and how they can inherit from each other - that I understand.

What I don't understand as yet is how all these classes can be stored in different files, doted around a folder structure, and yet all you need to do to use the class is just mention its name in your code. How does this work exactly?

And on a related note, if anyone can recommend a good book or online tutorial which will provide a good foundation in OOP (preferably php based) I'd be very grateful.

  • 写回答

9条回答 默认 最新

  • douweng3383 2009-04-24 19:28
    关注

    The technology answer is PHP Autoloading.

    The implementation answer is this:

    One common method used has to do with the names of classes relating to folder structure. There are articles out there, but here is a brief summary:

    When setting up your autoload code, take the class name and replace underscores with slashes. This way you can organize your classes in folders.

    For example:

    Classname: Database_Provider_MySQL

    File: Database/Provider/MySQL.php

    So in autoload, you'd take the incoming classname, replace the underscores with slashes. Then include that specific file.

    This achieves what you are trying to accomplish, you can simply load a class by creating a new instance of it. You never have to use the include statement for these classes.

    Do remember to not go to deep where you end up with 6+ levels. I think between 3 and 5 is a good maximum.

    Also, this does require that you keep only 1 class per file (similar to Java). Though it might seem inconvenient, it makes locating code a lot easier.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答