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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号