php OOP construct()参数

时间:2015-11-24 17:13:56

标签: php oop

我是OOP的新手。我一直在使用一个具有2个函数的类:

class department{
public $departmentid;
function __construct($todestroy){
    $this->departmentid = $todestroy;
}

function viewDeptFunct(){
    require_once "../db/class_db_connect.php";
    require_once "../config.php";
    $fetch_view_sql = "SELECT * FROM department WHERE id = '$this->departmentid'";
    $fetch_view_dept_sql = $db->prepare($fetch_view_sql);
    $fetch_view_dept_sql->execute();
    $fetch_view_dep_result = $fetch_view_dept_sql->fetch(PDO::FETCH_ASSOC);
    return $fetch_view_dep_result;
}

function editDeptFunct($name,$emailaddress,$password){
    require "../db/class_db_connect.php";
    require "../config.php";
    $edit_sql = "UPDATE department SET name = '$name', emailaddress = '$emailaddress', password = '$password' WHERE id = '$this->departmentid';";
    $edit_dept_sql = $db->prepare($edit_sql);
    $edit_dept_sql->execute();
}

我的实例化器是这样的:

$dept = new department($_GET['id']);
$dept_var = $dept->viewDeptFunct();
if(isset($_POST['edited'])){
$dept->editDeptFunct($_POST['name'],$_POST['emailaddress'],$_POST['password']);
}

我试图让实例化器工作,同时允许viewDeptFunct()函数也能工作,但我不知道如何工作。请有人可以告诉我。

我的想法是他们必须进入构造函数,但是viewDeptFunct()只允许1个参数?

1 个答案:

答案 0 :(得分:0)

从技术上讲,这个问题是无效的,因为修补程序与类或PHP没有任何关系。我忘记将name=""属性放在HTML表单中。一旦我把它们放到位并正确命名,一切都按预期工作。