使用 isset() 创建一个在构造函数中使用的通用函数? - PHP
我目前正在创建一个类来处理所有表单数据,然后将其放入数据库中以供将来使用。
目前,在我的类构造函数中,当我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我希望我没有误解——你是这个意思吗?
编辑:
如果你想设置类属性,怎么样?
I hope I'm not misunderstanding - is this what you mean?
Edit:
If you want to set class properties, how about this?
首先,这是一个语法错误(我猜你知道)
,这应该发出警告并导致其他行为,而不是你想要的行为,
你正在使用一个未定义的常量,在警告计算结果为字符串“second_parameter”之后,你 也应该使用该变量
,这里您使用的是字符串而不是变量:
另一个问题是您返回值,并且您没有将它们设置为类中的成员,
因此也许类似的内容
可能更接近您的内容需要
first of all, this is a sytax error (I guess you know)
also, this should issue an warning and lead to other behavior than what you want
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:
another problem is the fact that you return the values, and you don't set them as members in your class
so maybe something like
might be closer to what you need
这就是我可能会做的。
Here's what I might do.
它很长,但它会有所帮助......在这里我描述了所有函数并通过一个通用的 lib 文件添加。
类调用:
员工绩效类页面:
常用功能:
Its a lengthy, but it will help sure..here I describe all function and add by a common lib file.
class calling:
employee performance class page :
common function: