php oop编程:构建一个类

时间:2013-11-29 21:10:27

标签: php class oop

我正在寻找建立我的第一个真正的课程,我已经玩过一些比特和跳跃,但它是时候尝试真实了:)

我要做的是有一个表单类来处理我提交的所有表单,检查输入的数据并返回错误消息或成功消息。

所以这是我的一个表格,(我在1页上有5个)     

<form action="include/mform.php" method="post" name="business_listing">
    <input name="biz_name" type="text" value="Business or Place Name" />
    <select name="biz_department">
    <option value="">Business Sector</option>
        <?php
        $query = $user->database->query("SELECT * FROM tbl_sectors");
        while($row=$user->database->fetchArray($query))
        {
            $id = $row['sectorID'];
            $dept = $row['sectorName'];
            echo "<option value='$id'>$dept</option>";
        }?>
    </select>
    <input name="biz_address1" type="text" value="Street Name" />
                                <select name="job_location">
    <option value="">Location</option>
        <?php
        $query = $user->database->query("SELECT * FROM tbl_places");
        while($row=$user->database->fetchArray($query))
        {
            $id = $row['placeID'];
            $dept = $row['placeName'];
            echo "<option value='$id'>$dept</option>";
        }?>
    </select>

    <input name="biz_phone" type="text" value="Contact Number" />
    <input name="businessSubmit" type="submit" value="Submit" />
</form>
</div>

每个表单的操作都设置为包含/ mform.php,其中包含我的类。它在课堂上做的第一件事就是检查提交了哪个表格,然后想法检查已提交的数据,并用它做必要的事情

我的问题是,一旦我的班级知道提交了哪种表格,检查提交数据的最佳方法是什么?我应该在函数中创建变量来获取所有的后期数据并从那里获取它还是应该将实际函数中的那些作为参数传递? ,还是重要?

这是我当前的类文件,有点裸露

class Mform
{
    private $values = array();  //Holds submitted form field values
    private $errors = array();  //Holds submitted form error messages
    private $num_errors;   //The number of errors in submitted form

    public function __construct()
    {

        if(isset($_POST['businessSubmit']))
        {
            $this->chkBusiness();
        }

        if(isset($_POST['jobSubmit']))
        {
            $this->chkJob();
        }

        if(isset($_POST['accommodationSubmit']))
        {
            $this->chkAccommodation();
        }

        if(isset($_POST['tradeSubmit']))
        {
            $this->chkTrade();
        }

        if(isset($_POST['eventSubmit']))
        {
            $this->chkEvent();
        }

    }

    public function chkBusiness()
    {
        $field = "business";

    }

    public function chkJob()
    {
        return "job";
    }

    public function chkAccommodation()
    {
        return "accommodation";
    }

    public function chkTrade()
    {
        return "trade";
    }

    public function chkEvent()
    {
        return "event";
    }

    /**
    * setValue - Records the value typed into the given
    * form field by the user.
    */
    public function setValue($field, $value)
    {
        $this->values[$field] = $value;
    }

    /**
    * setError - Records new form error given the form
    * field name and the error message attached to it.
    */
    public function setError($field, $errmsg)
    {
        $this->errors[$field] = $errmsg;
        $this->num_errors = count($this->errors);
    }

    /**
    * value - Returns the value attached to the given
    * field, if none exists, the empty string is returned.
    */
    public function value($field)
    {
        if(array_key_exists($field,$this->values))
        {
            return htmlspecialchars(stripslashes($this->values[$field]));
        }
        else
        {
            return "";
        }
    }

    /**
    * error - Returns the error message attached to the
    * given field, if none exists, the empty string is returned.
    */
    public function error($field)
    {
        if(array_key_exists($field,$this->errors))
        {
            return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>";
        }
        else
        {
            return "";
        }
    }

    /* getErrorArray - Returns the array of error messages */
    public function getErrorArray()
    {
        return $this->errors;
    }

}

/* Initialize mform */
$mform = new Mform();

大多数单个函数只返回“word”作为占位符,所以我不会忘记以后再执行该功能。

这就是我想为每个单独的表单函数做的事情

    public function chkBusiness()
{
    $field = "business";
    $name = $_POST['biz_name'];// all need to be sanitized!!
    $dept = $_POST['biz_dept'];
    $address = $_POST['biz_address'];
    $location = $_POST['biz_location'];
    $phone = $_POST['biz_phone'];

    //start checking the input
    if(!$name || strlen($name = trim($name)) == 0)
    {
        $this->mform->setError($field, "* Name not entered");  
    }
    ...
    ...
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我为数据库中的每个表生成一个类;生命太短,实际上没有为每个数据库表编写该功能!

每个字段都有自己的对象,包含它的值,类型,最大长度等。字段也会添加到数组中,这样我就可以用它们做很酷的事情。

每个表类都扩展了一个更大的类,允许我插入,更新,删除和显示为表格和表单......我可以覆盖任何非标准的函数,尽管这种情况非常罕见。

对于迭代大约25000个大小合适的记录,性能开销约为0.001毫秒。

例如,我生成文档记录数据表的代码如下所示:

    //Create object
    $objDocument = new cls__CMS_Document();

    //Set the fields you want to display in the datatable
    $objDocument->objID->Active(true);
    $objDocument->objDocument_Name->Active(true);
    $objDocument->objAuthorID->Active(true);        
    $objDocument->objVersion->Active(true);        
    $objDocument->objDate_Created->Active(true);
    $objDocument->objDate_Last_Edited->Active(true);

    //Include a hyperlink from the ID field
    $objDocument->objID->HyperLink('/drilldown.php');
    $objDocument->objID->Post(true);

    //Pass a field through a formatting function
    $objDocument->objAuthorID->OutputFunction('getAuthorFromID');

    $result .= $objDocument->displayTable($sqlConditions);

    unset ($objDocument);

//Create object $objDocument = new cls__CMS_Document(); //Set the fields you want to display in the datatable $objDocument->objID->Active(true); $objDocument->objDocument_Name->Active(true); $objDocument->objAuthorID->Active(true); $objDocument->objVersion->Active(true); $objDocument->objDate_Created->Active(true); $objDocument->objDate_Last_Edited->Active(true); //Include a hyperlink from the ID field $objDocument->objID->HyperLink('/drilldown.php'); $objDocument->objID->Post(true); //Pass a field through a formatting function $objDocument->objAuthorID->OutputFunction('getAuthorFromID'); $result .= $objDocument->displayTable($sqlConditions); unset ($objDocument);

最好的一点:每一步,自动完成工作:)所以你键入$ objDocument-&gt;弹出所有方法和属性,包括所有字段对象,这样就不会拼错它们。

如果我得到足够的兴趣(投票),我会在网上提供全部内容。

虽然在制作自己的时候,这应该是值得深思的。