使用要在构造函数中使用的isset()创建泛型函数PHP
我目前正在创建一个类来处理我的所有表单数据,然后将这些数据放入数据库中供将来使用 目前,在我的类构造函数中,当我从POST构造变量时,我正在单独使用isset()函数使用要在构造函数中使用的isset()创建泛型函数PHP,php,function,isset,Php,Function,Isset,我目前正在创建一个类来处理我的所有表单数据,然后将这些数据放入数据库中供将来使用 目前,在我的类构造函数中,当我从POST构造变量时,我正在单独使用isset()函数 function __construct() { if (isset($_POST['first_name'])) { $this->first_name = $_POST['first_name']; }
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";
}
有人能给我指出正确的方向吗,或者带一个代码示例
我不确定的一个方面是如何设置函数以使用通用句柄处理两个参数
谢谢 我希望我没有误解-这是你的意思吗
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;
}
首先,这是一个sytax错误(我想你知道) 此外,这应该发出警告,并导致其他行为,而不是你想要的
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;
}
}
可能更接近您所需要的这是一个冗长的过程,但肯定会有帮助。.这里我描述了所有函数,并通过一个公共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");
}
?>
我可以这样做
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";
}
}
在
$\u POST
变量中定义一个带有所需变量名和相关键的关联数组怎么样?您可以循环数组并检查每个值,而无需克隆代码。但它如何知道$first\u参数?它是否只是获取函数调用中的任何内容,并在函数中需要的地方使用它?我在想,这可能是类成员的名称。。所以还有一条线。。你知道的。。对不起,如果我误解了我的意思是你会像这样使用$this->setter('firstname','fname');其中'firstname'是类成员的名称,'fname'是$\u POST Arrayah中的键好的,谢谢,我想在你的好例子之后,我会做的是,可能只是拥有我的POST数组的键,然后在构造函数中这样做:$this->first\u name=$this->setter('first\u name');谢谢你的鼓励和帮助!:)@不客气,如果有帮助,我很高兴。仔细看看我的意思就是这个例子:在任何疑问或描述中,你能问我吗?
<?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;
}
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";
}
}