【问题标题】:Add attributes/ parameters to existing method in a class向类中的现有方法添加属性/参数
【发布时间】:2015-11-02 03:34:44
【问题描述】:

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

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!";
        }
    }
}

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

【问题讨论】:

  • 您的代码看起来不完整,因为它不包含类定义。只需将另一个函数添加到您的类中以获得新方法。
  • 我只想为这个方法添加两个属性,不需要继承,只是将参数从 html 表单元素传递给这个函数时遇到问题。我的判断是对的还是除此之外还有其他需要考虑的事情
  • 您最初的问题是如何向类添加方法。然后您上面的评论询问如何向方法添加属性(没有意义)并且在传递参数时遇到问题。我认为您需要阅读类、方法、参数和参数的定义。如果(如我所料)您在问“如何向此函数/方法添加 2 个参数?”那么请改写问题。
  • 尊敬的先生,我是编程新手,对 PHP 中的参数传递了解不多。如果您能解释如何将参数传递给函数,那将会更有帮助。我的意思是你的预期是非常正确的......
  • 我已经为我现在认为是您的问题添加了答案。如果您可以更新此问题以反映您的实际要求,将会很有帮助。

标签: php class methods parameters


【解决方案1】:

通常方法/功能是按照以下方式定义的

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;
}

您还需要将额外的属性添加到课程中,但由于您尚未在此处发布课程详细信息,因此目前无法为您提供帮助。

抱歉,如果这不是您的要求。

【讨论】:

  • 我已经尝试设置员工工资并像这样从 html 代码中获取其值,但不起作用code staff_salary($admin_update->staff_salary);? > ">
  • 我没有在这里提到类代码,因为它继承自另一个类并且是派生类,如果你能理清我缺少的东西,那么请告诉我我会发布整个类代码
  • 我希望您使用对象引用而不是 $_POST 数组。乍一看,您的括号似乎也放错了位置。不应该是&lt;?php echo (isset($_POST['salary']) ? $_POST['salary'] : ""); ?&gt; 在任何情况下,我认为您需要隔离您的问题以查看它是否在存储值或检索它们时发生。请参阅 blog.teamtreehouse.com/how-to-debug-in-php 或 google debugging php 以获得更多帮助。
  • 我在存储它们时遇到问题感谢您的贡献,如果老师能以这种方式提供帮助,我感到很惊讶。你真棒,先生。帽子......我没有说谎......
  • 您现在应该拥有解决问题所需的所有信息。如果没有更多详细信息,我认为我们无法为您提供更多帮助。
【解决方案2】:

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

如果您采用继承方法,则需要更新任何实例化该类的代码,并使其根据需要实例化您的类。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-10-23
    • 2012-12-14
    相关资源
    最近更新 更多