dongtiran7769 2014-11-22 17:43
浏览 45
已采纳

用于AJAX调用和表单操作的PHP文件夹结构

I am trying to develope good code organization habits and work exclusively with OOP in php but I can't seem to wrap my head around something.

Here is a simplified description of what I am working with:

  • I have all my class files in a folder '/resources/Classes'
  • I have all my html and javascript in '/public_html' & '/public_html/script respectively'

My question is concerning files that are the actions of forms or AJAX requests. For example 'formAction.php' and 'ajaxURL.php'. These files are not Classes and also do not contain any html or other such GUI.

I have been putting them in a folder 'resources/actions' but my gut tells me something about this is not fully OOP.

Is my usage of these files incorrect if I am trying for complete OOP? if so how can I approach this differently.

Here is an actual file from my project as a concrete example:

//file: getBalance.php

<?php
/**
 * This script gets the balance of an account from the server
 */

if (!isset($Database)) {
    $Database = require_once "../clear_finance_pkg.php";
}

/** @var User $User */
$User = $Database->getUserByID("1");//TODO: user should be set dynamically

$User->setAccounts($Database->getAccountsByUser($User));

if (isset($arg1)) {
    $accountID = $arg1;

    foreach ($User->getAccounts() as $Account) {

        if ($Account->getId() == $accountID) {

            $RbcChequing = RbcAccount::accountToRbcAccount($Account, "Chequing");

            echo '$' . Money::toDollars($RbcChequing->getBalance());
            break;
        }


    }
} else throw new Exception('Account ID is not set. Could not get balance');
  • 写回答

1条回答 默认 最新

  • dongyue4964 2014-12-03 14:59
    关注

    It's difficult to say if your code is complete OOP, but i think it isn't. It looks like you are on the right track, because you are using classes, objects and methods. That's the basic of OOP. No longer large if/else statements and a lot of variables that doesn't make sense, but objects and methods without code-duplication.

    I think your question in more related to the seperation of logic and view. First of all. In general it's ok to make a file for a function, but in a large application you will loose the overview. What you are doing know is combine view-related and logic-related things in one file, but actually that's not what you want. The ideal situation is full seperation of logic and view so you can create multiple seperate views based on the same logic.

    My advice is to take a look at the MVC-pattern. Take a look at this link. It will help you to get a basic understanding of the MVC-pattern. Notice that you won't longer need to have a file for each function. And you have a seperation of your logic and view elements because you can use the same model in multiple views (although this is maybe not the best example).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?