php OOP construct() arguments


php OOP construct() arguments

我是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个参数,怎么办?

从技术上讲,这个问题是无效的,因为修复与类或PHP无关。我忘了把name=""属性放在HTML表单中。一旦我把它们放在适当的位置并正确命名,一切都如预期的那样工作。