我有一个类admin用户,它有一些修改用户数据的权限。我想向该方法添加两个或更多属性,以完成所需的功能。
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!";
}
}
}
我如何在这里添加新的参数代码?
您的选择是编辑类本身,或者编写一个继承类并实现您的两个额外方法的新类。
如果您选择继承方法,那么您将需要更新任何实例化该类的代码,并使其根据需要实例化您的类。
http://php.net/manual/en/language.oop5.inheritance.php通常情况下,方法/函数是按照
这一行定义的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 = "a"
- 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;
}
你还需要为类添加额外的属性,但由于你没有在这里发布类的详细信息,我目前无法帮助你。
如果这不是你想要的,请原谅。