使用 isset() 创建一个在构造函数中使用的通用函数? - PHP

发布于 2024-12-20 15:55:12 字数 1210 浏览 1 评论 0原文

我目前正在创建一个类来处理所有表单数据,然后将其放入数据库中以供将来使用。

目前,在我的类构造函数中,当我从 POST 构造变量时,我正在单独使用 isset() 函数。

function __construct()
    {
            if (isset($_POST['first_name']))
            {
                 $this->first_name = $_POST['first_name'];
            }
            else
            {
                 $this->first_name = "NULL";
            }

    }

尽管此示例仅针对一个变量,但我正在为我的 php 应用程序中的另外 12 个变量执行此操作。

正如古老的格言所说“你不能重复代码”。

因此,我尝试在类中创建一个能够处理所有变量的成员函数,例如 get/set 函数。

因此,在我的构造函数中,我需要做的就是:

function __construct()
    {
            //first parameter is the member variable to be set, second parameter is the element name that the value will be POST from
           $firstname = $this->setter($firstname, "first_name");

    }

//what do I put here?
    function setter(first parameter, second parameter)
        {
                //checks the post variable has been set
                if ( isset($_POST[second_parameter]))
                return $_POST['second_parameter'];  
                else
                return "NULL";

        }

有人能指出我正确的方向,或者使用代码示例吗?

我不确定的方面是如何设置函数来使用通用句柄处理两个参数?

谢谢!

I am currently creating a class to handle all my form data which will then be placed into a database for future use.

Currently in my class constructor I am using the isset() function on an individual basis when I construct my variables from POST.

function __construct()
    {
            if (isset($_POST['first_name']))
            {
                 $this->first_name = $_POST['first_name'];
            }
            else
            {
                 $this->first_name = "NULL";
            }

    }

Although this example is just for one variable, I am doing this for another 12 in my php application.

As the old adage says "You cannot repeat code".

So I am trying to create a member function in my class that will be able to handle all variables, like a get/set function.

So that in my constructor all I need to do is this:

function __construct()
    {
            //first parameter is the member variable to be set, second parameter is the element name that the value will be POST from
           $firstname = $this->setter($firstname, "first_name");

    }

//what do I put here?
    function setter(first parameter, second parameter)
        {
                //checks the post variable has been set
                if ( isset($_POST[second_parameter]))
                return $_POST['second_parameter'];  
                else
                return "NULL";

        }

Can someone point me in the right direction, or with a code sample.

The aspect I am not sure about is how do I set the function to handle two parameters with a generic handle?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸短情长 2024-12-27 15:55:12

我希望我没有误解——你是这个意思吗?

function __construct() {
       $firstname = $this->setter("first_name");
}

function setter($postKey) {
            if (isset($_POST[$postKey])) {
              return $_POST[$postKey];  
            }
            return NULL;
}

编辑:

如果你想设置类属性,怎么样?

function __construct() {
      $this->setter("first_name", "firstName"); // Post key, class property
}

function setter($postKey, $classProperty) {
            if (isset($_POST[$postKey])) {
              return $this->{$classProperty} = $_POST[$postKey];
            }
            return $this->{$classProperty} = NULL;
}

I hope I'm not misunderstanding - is this what you mean?

function __construct() {
       $firstname = $this->setter("first_name");
}

function setter($postKey) {
            if (isset($_POST[$postKey])) {
              return $_POST[$postKey];  
            }
            return NULL;
}

Edit:

If you want to set class properties, how about this?

function __construct() {
      $this->setter("first_name", "firstName"); // Post key, class property
}

function setter($postKey, $classProperty) {
            if (isset($_POST[$postKey])) {
              return $this->{$classProperty} = $_POST[$postKey];
            }
            return $this->{$classProperty} = NULL;
}
没有伤那来痛 2024-12-27 15:55:12

首先,这是一个语法错误(我猜你知道)

function setter(first parameter, second parameter)

,这应该发出警告并导致其他行为,而不是你想要的行为,

isset($_POST[second_parameter]

你正在使用一个未定义的常量,在警告计算结果为字符串“second_parameter”之后,你 也应该使用该变量

,这里您使用的是字符串而不是变量:

return $_POST['second_parameter']; 

另一个问题是您返回值,并且您没有将它们设置为类中的成员,

 if ( isset($_POST[second_parameter]))
                return $_POST['second_parameter'];  
                else
                return "NULL";

因此也许类似的内容

function setter($first_parameter, $second_parameter)
{
        //checks the post variable has been set
        if ( isset($_POST[$second_parameter]))
        {
            $this->$first_parameter = $_POST[$second_parameter];  
        }
        else
        {
            $this->$first_parameter = NULL;
        }
}

可能更接近您的内容需要

first of all, this is a sytax error (I guess you know)

function setter(first parameter, second parameter)

also, this should issue an warning and lead to other behavior than what you want

isset($_POST[second_parameter]

you are using an undefined constant that after the warning evaluates to the string "second_parameter", you should use the variable

also, here you are using a string and not a variable:

return $_POST['second_parameter']; 

another problem is the fact that you return the values, and you don't set them as members in your class

 if ( isset($_POST[second_parameter]))
                return $_POST['second_parameter'];  
                else
                return "NULL";

so maybe something like

function setter($first_parameter, $second_parameter)
{
        //checks the post variable has been set
        if ( isset($_POST[$second_parameter]))
        {
            $this->$first_parameter = $_POST[$second_parameter];  
        }
        else
        {
            $this->$first_parameter = NULL;
        }
}

might be closer to what you need

抠脚大汉 2024-12-27 15:55:12

这就是我可能会做的。

class Person {
  function __construct(){
    $keystomatch = array("fname", "lname", "addr1", "addr2");
    foreach($_POST as $k => $v){
        if(!in_array($k,$keystomatch)) continue;
        $this->$k = $v;
    }
    foreach($keystomatch as $p) 
        if(!isset($this->$p)) $this->$p = "NULL";
  }
}

Here's what I might do.

class Person {
  function __construct(){
    $keystomatch = array("fname", "lname", "addr1", "addr2");
    foreach($_POST as $k => $v){
        if(!in_array($k,$keystomatch)) continue;
        $this->$k = $v;
    }
    foreach($keystomatch as $p) 
        if(!isset($this->$p)) $this->$p = "NULL";
  }
}
北渚 2024-12-27 15:55:12

它很长,但它会有所帮助......在这里我描述了所有函数并通过一个通用的 lib 文件添加。

类调用:

    <?php

     if(isset($_POST) && isset ($_POST["form_submit"])){
     $valArr=array("first name"=>$_POST['first_name '],"2nd paremeter"=>$_POST['2nd value']..and more);
     $per_obj = new Performance();
    $per_obj->addPreformance($valArr, "employee_performance");

   }
  ?>

员工绩效类页面:

   <?php 

   class Performance extends DataAccess{

       var $db_obj = null;
     public function  __construct() {
         $this->db_obj = new DataAccess();
    }

   public function  addPreformance($key_values = array(), $table = null) {
    $this->db_obj->table_name = $table;
    $this->db_obj->addRecord($key_values);
  $msg=$this->db_obj->msg;
header("Location: performance.php?msg=$msg");
}
   }

?>

常用功能:

     function addRecord($key_values)
    {

        $cols="";
        $vals="";
        foreach($key_values as $key=>$value)
        {
            if ($key!="submit" and $key!="PHPSESSID" and $key!="image_name" and $key!="submit_button" and $key!="ext" and $key!="ext2" and $key!="img_name" and $key!="mode" and $value!="" and $key!="gpl" and $key!="ip1" and $key!="ip2" and $key!="ip3" and $key!="ip4"){
                $cols .= "`".$key."`,";
                is_string($value)? $vals .= "'".addslashes($value)."'," : $vals .= "'".$value."',";

            }
        }


        $cols = substr($cols, 0, -1);
        $vals = substr($vals, 0, -1);

        $insert_qry="insert into ". $this->table_name ."(". $cols .") values(". $vals   .")";

        $r=mysql_query($insert_qry);
        $this->msg = (!$r) ? f_add_msg : s_add_msg;
        return $r;
    }

Its a lengthy, but it will help sure..here I describe all function and add by a common lib file.

class calling:

    <?php

     if(isset($_POST) && isset ($_POST["form_submit"])){
     $valArr=array("first name"=>$_POST['first_name '],"2nd paremeter"=>$_POST['2nd value']..and more);
     $per_obj = new Performance();
    $per_obj->addPreformance($valArr, "employee_performance");

   }
  ?>

employee performance class page :

   <?php 

   class Performance extends DataAccess{

       var $db_obj = null;
     public function  __construct() {
         $this->db_obj = new DataAccess();
    }

   public function  addPreformance($key_values = array(), $table = null) {
    $this->db_obj->table_name = $table;
    $this->db_obj->addRecord($key_values);
  $msg=$this->db_obj->msg;
header("Location: performance.php?msg=$msg");
}
   }

?>

common function:

     function addRecord($key_values)
    {

        $cols="";
        $vals="";
        foreach($key_values as $key=>$value)
        {
            if ($key!="submit" and $key!="PHPSESSID" and $key!="image_name" and $key!="submit_button" and $key!="ext" and $key!="ext2" and $key!="img_name" and $key!="mode" and $value!="" and $key!="gpl" and $key!="ip1" and $key!="ip2" and $key!="ip3" and $key!="ip4"){
                $cols .= "`".$key."`,";
                is_string($value)? $vals .= "'".addslashes($value)."'," : $vals .= "'".$value."',";

            }
        }


        $cols = substr($cols, 0, -1);
        $vals = substr($vals, 0, -1);

        $insert_qry="insert into ". $this->table_name ."(". $cols .") values(". $vals   .")";

        $r=mysql_query($insert_qry);
        $this->msg = (!$r) ? f_add_msg : s_add_msg;
        return $r;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文