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 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码