将属性/参数添加到类中的现有方法

时间:2015-08-10 09:19:25

标签: php class methods parameters

我有一个类管理员用户,它有权修改用户数据。我想为此方法添加两个或更多属性以完成所需的功能。

function update_user_by_admin($new_level, $user_id, $def_pass, $new_email, $active, $confirmation = "no", $staff_salary, $set_duty_timings) {
    $this->user_found = true;
    $this->user_access_level = $new_level;
    $this->set_staff_salary=$staff_salary;
    if ($def_pass != "" && strlen($def_pass) < 4) {
        $this->the_msg = "Password is too short use the min. of 4 chars.";
    } else {
        if ($this->check_email($new_email)) {
            $sql = "UPDATE %s SET access_level = %d, email = '%s', active = '%s', salary='salary'";
            $sql .= ($def_pass != "") ? sprintf(", pw = '%s'", md5($def_pass)) : "";
            $sql .= " WHERE id = %d";
            $sql_compl = sprintf($sql, $this->table_name, $new_level, $new_email, $active, $user_id);
            if (mysql_query($sql_compl)) {
                $this->the_msg = "Data is modified for user with id#<b>".$user_id."</b>";
                if ($confirmation == "yes") {
                    if ($this->send_confirmation($user_id)) {
                        $this->the_msg .= "<br>...a confirmation mail has been sent to the user.";
                    } else {
                        $this->the_msg .= "<br>...ERROR no confirmation mail is sent to the user.";
                    }
                }
            } else {
                $this->the_msg = "Database error, please try again!";
            }
        } else {
            $this->the_msg = "The e-mail address is invalid!";
        }
    }
}

如何在此处添加新参数代码?

2 个答案:

答案 0 :(得分:1)

您的选择是编辑类本身,或编写从类继承的新类并实现您的两个额外方法。

如果你选择继承方法,那么你需要更新任何实例化该类的代码,并使其根据需要实例化你的类。

http://php.net/manual/en/language.oop5.inheritance.php

答案 1 :(得分:0)

通常,方法/功能是按照

的方式定义的
function my_function_name($param1, $param2, $param3, etc, etc...) {
  // do something with $param1
  // do something with $param2
  // do something with $param3
}

方法的参数定义为逗号分隔列表。调用函数时,您将参数(即实际值)传递给函数。要允许传递更多参数,只需在逗号分隔列表中添加更多参数,然后就可以在函数中执行任何必要的工作。

例如,您的功能签名目前是:

function update_user_by_admin($new_level, $user_id, $def_pass, $new_email, $active, $confirmation = "no", $staff_salary, $set_duty_timings) {
// do some stuff...
}

添加更多参数,您只需扩展功能线。例如:

function update_user_by_admin($new_level, $user_id, $def_pass, $new_email, $active, $confirmation = "no", $staff_salary, $set_duty_timings, $another_param, $one_more) {
// do some stuff...
}

如果您正在询问如何将参数(接收到参数中的数据)传递给函数/方法,那么您只需指定函数名称后跟逗号分隔的值列表(当然,最后需要使用分号) PHP中的声明)。例如

my_function_name("a",2,TRUE);

会导致:

  • param1 =&#34; a&#34;
  • param2 = 2
  • param3 = TRUE

更详细地查看您的代码我可以看到方法参数正用于更新类的属性。所以你想最终得到类似的东西:

function update_user_by_admin($new_level, $user_id, $def_pass, $new_email, $active, $confirmation = "no", $staff_salary, $set_duty_timings, $another_param, $one_more) {
    $this->user_found = true;
    $this->user_access_level = $new_level;
    $this->set_staff_salary=$staff_salary;
    // lots of other stuff
    $this->another_prop = $another_param;
    $this->one_more_prop = $one_more;
}

你还需要在课堂上添加额外的属性,但是由于你没有在这里发布课程详情,我现在无法帮助你。

道歉,如果这不是你要求的。