如何扩展多个类

时间:2011-06-25 09:31:01

标签: php

我的申请结构:

E:\ wamp \ www \ mobile \ module \ mod_expense.php CLASS NAME:mod_expense说明:包含INSERT,DELETE,UPATE操作以及此clas扩展con_class CLASS

E:\ wamp \ www \ mobile \ controller \ _control_busniss.php CLASS NAME:con_class说明:包含选择查询业务逻辑

E:\ wamp \ www \ mobile \ view \ expenditure \ expense.php 说明:包含expense.html CLASS和contr_busniss.php CLASS目的:使用此文件运行应用程序,例如要显示费用添加表格,那么我的网址看起来像 http://localhost/mobile/view/expenditure/expense.php?fp = add 费用查看表格,然后我的网址看起来像http://localhost/mobile/view/expenditure/expense.php?fp=view

E:\ wamp \ www \ mobile \ view \ expenditure \ expense.html.php CLASS NAME:expense_html_form说明:包含FORM(ADD费用,编辑费用,查看费用)以及此clas扩展con_class CLASS

现在我有数据库类:CLASS Name是DB_Class,我想在mod_expense.php&中使用这些类函数。 expense.html.php,我对OOPS并不太熟悉,

目前这些文件已经扩展了con_class CLASS,现在我想扩展Database类,如何在这里扩展DB CLASS,

我的片段很少:

文件名:expense.php

include("../../include/config.php");
include("../../include/dbclass.php");
include_once("expense.html.php");
include("../../controller/con_error.php");
include_once("mod_expense.php");
 $obj_dbclass = new dbClass();
 $obj_dbclass->db_connect();


 $expense_object = new expense_html_form();
 $expense_object->expense_add();
 if(isset($_POST['submit'])){
    $mod_expense = new mod_expense();
    echo $mod_expense->ins_expense();
 }

文件名:expense.html.php

class expense_html_form{

    function expense_add(){
        ADD FORM
    }
}

文件名:mod_expense.php

class mod_expense extends con_class{
    function ins_expense(){
         EXPENSE ADD FUNCTIONALITY
    }
}

文件名:contr_busniss.php

class con_class{  

    function check_already_exist($amount){
echo "SELECT expense_amount FROM expense WHERE expense_amount = '$amount'";
        $select=mysql_query("SELECT count(expense_amount) FROM expense WHERE expense_amount = '$amount'");
                $row=mysql_fetch_array($select);
                $op=$row[0];
                return $op;
    }
}

现在我想扩展DBClass函数。建议如何实现。

1 个答案:

答案 0 :(得分:0)

我认为,当你的情况更好时,你可能会尝试用继承来完成太多。您可以将$dbClass的引用传递给控制器​​,以便它们可以访问数据库功能。

从您的类的名称我假设您正在尝试使用模型 - 视图 - 控制器,在这种情况下,我建议不要将DB_class功能公开给expense_html_form。您应该在控制器中执行任何必要的计算,并将最终结果提供给视图。

如果您绝对必须使用继承,则expense_html_formcon_class扩展DB_class会将这些方法公开给expense_html_formmod_expense