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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c