如何跨类中的方法传递构造函数GET参数?PHP
我基本上是试图将GET值从构造函数传递给insert\u update函数。我已经去掉了后一个函数的很多部分。另外,如果值得注意的话,这个页面是一个包含页面如何跨类中的方法传递构造函数GET参数?PHP,php,oop,class,function,methods,Php,Oop,Class,Function,Methods,我基本上是试图将GET值从构造函数传递给insert\u update函数。我已经去掉了后一个函数的很多部分。另外,如果值得注意的话,这个页面是一个包含页面 class Updates { function __construct(){ $owner = $this->get = array_map('mysql_real_escape_string', $_GET); $owner=$owner["member_id"]; echo $owner; } //Insert Updat
class Updates {
function __construct(){
$owner = $this->get = array_map('mysql_real_escape_string', $_GET);
$owner=$owner["member_id"];
echo $owner;
}
//Insert Update
public function Insert_Update()
{
$query = mysql_query("INSERT INTO `field` (foo) VALUES (N'$owner')") or die(mysql_error());
}
您可以使
owner
成为Updates
的成员,并将其存储在this->owner
中
class Updates {
private $owner;
function __construct(){
$owner = $this->get = array_map('mysql_real_escape_string', $_GET);
$this->owner=$owner["member_id"];
echo $this->owner;
}
//Insert Update
public function Insert_Update()
{
$query = mysql_query("INSERT INTO `field` (foo) VALUES (N'".$this->owner."')") or die(mysql_error());
}
}
*抱歉,我编辑了几次,复制和粘贴不正确,然后我想说得更清楚。我有点困惑。$owner变量在构造函数中的作用是什么?您想从构造函数中的get获取成员\u id并在insert\u update方法中使用它吗?这正是我想做的。在SQL查询中,您想从post中获取所有内容吗?或者仅仅是成员id?为什么不在插入更新中使用$this->get['member\u id']而不使用构造函数呢?如果不调用构造,我如何获取get参数?如果不调用
插入更新()
,就不能调用\u构造()
首先。我如何让它成为更新的成员?其他答案告诉你:private$owners代码><代码>$this->owners=$owners
。那么我应该让它同时成为构造和插入更新的成员?我试过了,但也没用。所以,使用这个:protected$owner;函数{$this->owner=array\u map($mysql\u real\u escape\u string',$\u GET);echo$this->owner;}SQL插入:{.$this->owner.}返回一个数组,关于如何获得实际的GET值有什么想法吗?这取决于传递给构造函数的内容。在您提供的示例中,您似乎想要抓住$owner[“member_id”]
,我简化了我的示例,以便更清楚地理解这一点。为了使我的示例起作用,您可以通过传递所需的变量来实例化该类。然后,可以对构造函数中的值进行任何清理<代码>$updates=新更新($\u GET[“member\u id”])然后使用您喜欢的任何方法清理构造函数中的值,例如mysql\u real\u escape\u string
$this->owner=array\u map('mysql\u real\u escape\u string',$\u GET);-这条线负责。array\u map返回array,您正在将这个返回的数组分配给“owner”字段。实际上我重新检查了它,问题是数组本身是空的;我刚在Insert_Update中得到一个空数组。您是否声明“owner”为类成员?我在我的答案中更新了代码,看第2行。我复制并尝试了这个,但出于某种原因,它只在数据库中给了我一个空白。但是echo$this->owner;从构造函数显示正确的字符串?你能显示这个回声吗?尝试在sql查询之前添加“echo$this->owner;”,并检查是否有正确的值;from构造函数显示正确,但在sql查询之前的Insert_更新中,我无法使用相同的代码显示它。
class Updates {
protected $owner;
function __construct($myGetVariable)
$this->owner = $myGetVariable;
}
//Insert Update
public function Insert_Update()
{
// you can access the variable by typing:
echo $this->owner;
$query = mysql_query("INSERT INTO `field` (foo) VALUES (N'".$this->owner."')") or die(mysql_error());
}