doujiao3072 2011-04-23 10:49
浏览 42
已采纳

PHP和服务器层次结构设计,受到url传播更改的启发

I had a general question on proper design of a php files and their storage on a server.

The problem is this: I had split a php object's functions into different php files, something like:

File 1 AndroidFlashCard.php

class AndroidFlashCard {
    public function retrieveCards($packname){}
    public function retrievePacks(){}
    ....

File 2 RetrieveCards.php

include ($_SERVER['DOCUMENT_ROOT'].'/flash_card/AndroidFlashCard.php');
$connection = new AndroidFlashCard();
$connection->retrieveCards($_REQUEST['pack']);
...

Besides the bad code smell regarded making seperate php files for a single function calls, the problem happens when the location/name of class AndroidFlashCard changes. Suppose we go with this shoddy design and I have 1000 different functions...

The quick and dirty solution that came to mind was to have ANOTHER include file:

File 3 include.php:

include ($_SERVER['DOCUMENT_ROOT']./[location of class])

But this doesn't really change anything, because if the location of the include.php file changes, I'd have to make 1000 changes again.

So let's think about this. If I have to keep making 1000 changes to 1000 php files that only include a reference to a class and then code to execute a function, then maybe THAT is the design problem.

In android, I only know how to execute http requests, which is why I split the function calls into separate files.

If I could get hold of that php object, it would make things easier, but I've a feeling this will be difficult to accomplish.

What is the simpler solution? Is writing directory structures a design part of production work, which after approval, is simply just written in stone?

  • 写回答

2条回答 默认 最新

  • doutou7740 2011-04-23 11:05
    关注

    This sounds like a good case for class autoloading. When you attempt to use the class in say file 2, then php can call an autoload function to actually find the class to include. You will have to write the logic behind it, but thats 1 change rather then a 1000 as you say.

    __autoload($class_name)

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看