duanlongling5308 2012-10-02 21:55 采纳率: 0%
浏览 21
已采纳

程序PHP到哪里?

All of the code in my project is procedural. It isn't using any framework, or any MVC structure. Most of the PHP is form handlers with some logic. So lots of code like this:

<?php

require "headerFile.php";

$user = array(); // filled with user data

if (isset($_POST['submitButton'])) {
    if (isset($_POST['money'])) {
        if (is_numeric($_POST['money'])) { // I don't actually validate numbers this way
            if ($_POST['money'] <= $user['money']) {
                $query = mysql_query("SELECT * FROM someTable WHERE id={$user['id']}");

                if($result = mysql_fetch_array($query)) {
                    if ($someOtherCheck == $user['someOtherData']) {
                        /*
                         * run some queries, give user some item
                         *
                         */
                    } else {
                        echo "This isn't right.";
                    }
                } else {
                    echo "You don't have a row in some table!";
                }
            } else {
                echo "You don't have that much money!";
            }
        } else {
            echo "Enter a valid number";
        }
    } else {
        echo "Enter how much you want to wager.";
    }
}

// some hard coded form goes here

require "footerFile.php";

?>

There are over a hundred of these forms and nested if handlers, some that are similar with more/fewer conditions.

I want to start using OOP and I've been reading up on design patterns but I can't seem to find anything that applicable to my project.

I'm adding loads of new blocks of code similar to the block above, I don't want to copy and paste and just change a couple of bits here and there, what are my options?

  • 写回答

7条回答 默认 最新

  • donh61500 2012-10-02 22:55
    关注

    I want to start using OOP and I've been reading up on design patterns but I can't seem to find anything that applicable to my project.

    You don't have to start with patterns yet .. understand the basics and you can progress form there ..

    This is a basic example

    if (isset($_POST['submitButton'])) {
    
        $db = new \mysqli("localhost", "user", "pass", "db");
    
        $user = new User("Juddling Stack", 123456);
        $user->setMoney(500.01);
    
        $player = new PlayerRegister($db, $user);
    
        if (! isset($_POST['money']) || ! is_numeric($_POST['money']))
            $player->addError("Enter Valid Wager Money");
    
        if ($_POST['money'] <= $user['money']) {
            $player->addError("You don't have that much money!");
        }
    
        try {
            $player->doSQL();
        } catch ( BaseException $e ) {
    
            foreach ( $e->getError() as $error ) {
                echo $error, " <br />";
            }
        }
    }
    

    Classes Used

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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用