将属性传递给PHP方法(Passing property to PHP method)

我有以下类,我在其中添加了属性$user 。

include_once(__CA_LIB_DIR__."/ca/Search/BaseSearch.php"); include_once(__CA_LIB_DIR__."/ca/Search/ObjectSearchResult.php"); class ObjectSearch extends BaseSearch { # ---------------------------------------------------------------------- /** * Which table does this class represent? */ protected $ops_tablename = "ca_objects"; protected $ops_primary_key = "object_id"; public $user; # ---------------------------------------------------------------------- public function &search($ps_search, $pa_options=null, $user) { return parent::doSearch($ps_search, new ObjectSearchResult(), $pa_options); } # ---------------------------------------------------------------------- } ?>

在下面的代码中,我无法将$user属性传递给搜索方法。 我尝试使用$user , $this->user和new ObjectSearch($user) 。 刚接触PHP我知道我问的是一个天真的问题,但我不能自己解决,相信我,我试了几天。 我怎么能做到这一点?

$po_request = $this->getVar('request'); $vs_widget_id = $this->getVar('widget_id'); $user = $this->getVar('user'); $o_search = new ObjectSearch(); $result = $o_search->search('created.$user.:"2013"'); $count = 1; while($result->nextHit()) { print "Hit ".$count.": "."<br/>\n"; print "Idno: ".$result->get('ca_objects.idno')."<br/>\n"; print "Name: ".$result->get('ca_objects.preferred_labels.name')."<br/>\n"; $count++; } ?>

I have the following class, where I added the property $user.

include_once(__CA_LIB_DIR__."/ca/Search/BaseSearch.php"); include_once(__CA_LIB_DIR__."/ca/Search/ObjectSearchResult.php"); class ObjectSearch extends BaseSearch { # ---------------------------------------------------------------------- /** * Which table does this class represent? */ protected $ops_tablename = "ca_objects"; protected $ops_primary_key = "object_id"; public $user; # ---------------------------------------------------------------------- public function &search($ps_search, $pa_options=null, $user) { return parent::doSearch($ps_search, new ObjectSearchResult(), $pa_options); } # ---------------------------------------------------------------------- } ?>

In the following code I can't pass the $user property to the search method. I tried with $user, $this->user and new ObjectSearch($user). Being new to PHP I know I'm asking a naive question, but I can't solve it by myself, believe me I tried for days. How can I accomplish this?

$po_request = $this->getVar('request'); $vs_widget_id = $this->getVar('widget_id'); $user = $this->getVar('user'); $o_search = new ObjectSearch(); $result = $o_search->search('created.$user.:"2013"'); $count = 1; while($result->nextHit()) { print "Hit ".$count.": "."<br/>\n"; print "Idno: ".$result->get('ca_objects.idno')."<br/>\n"; print "Name: ".$result->get('ca_objects.preferred_labels.name')."<br/>\n"; $count++; } ?>

最满意答案

public function &search($ps_search, $pa_options=null, $user)

它有一些问题:

在具有默认值的参数之后传递没有默认值的参数没有任何意义 你必须在这里传递第三个参数(你只传递一个) 您不必手动传递类属性; 他们自动在$this

所以写:

public function &search($ps_search, $pa_options=null) { return parent::doSearch($ps_search, new ObjectSearchResult($this->user), $pa_options); }

或者您可能需要$user class属性,只需编写$this->user 。

$this始终在对象上下文中设置:您不需要自己传递它。

public function &search($ps_search, $pa_options=null, $user)

There are a few things wrong with it:

It doesn't make any sense to pass a parameter without default value after a parameter which has a default value You have to pass the third parameter here (you only pass one) You don't have to pass the class properties manually; they're automatically in $this

So write:

public function &search($ps_search, $pa_options=null) { return parent::doSearch($ps_search, new ObjectSearchResult($this->user), $pa_options); }

Or where ever you may need your $user class property, write simply $this->user.

$this is always set in object context: you don't need to pass it yourself.

将属性传递给PHP方法(Passing property to PHP method)

我有以下类,我在其中添加了属性$user 。

include_once(__CA_LIB_DIR__."/ca/Search/BaseSearch.php"); include_once(__CA_LIB_DIR__."/ca/Search/ObjectSearchResult.php"); class ObjectSearch extends BaseSearch { # ---------------------------------------------------------------------- /** * Which table does this class represent? */ protected $ops_tablename = "ca_objects"; protected $ops_primary_key = "object_id"; public $user; # ---------------------------------------------------------------------- public function &search($ps_search, $pa_options=null, $user) { return parent::doSearch($ps_search, new ObjectSearchResult(), $pa_options); } # ---------------------------------------------------------------------- } ?>

在下面的代码中,我无法将$user属性传递给搜索方法。 我尝试使用$user , $this->user和new ObjectSearch($user) 。 刚接触PHP我知道我问的是一个天真的问题,但我不能自己解决,相信我,我试了几天。 我怎么能做到这一点?

$po_request = $this->getVar('request'); $vs_widget_id = $this->getVar('widget_id'); $user = $this->getVar('user'); $o_search = new ObjectSearch(); $result = $o_search->search('created.$user.:"2013"'); $count = 1; while($result->nextHit()) { print "Hit ".$count.": "."<br/>\n"; print "Idno: ".$result->get('ca_objects.idno')."<br/>\n"; print "Name: ".$result->get('ca_objects.preferred_labels.name')."<br/>\n"; $count++; } ?>

I have the following class, where I added the property $user.

include_once(__CA_LIB_DIR__."/ca/Search/BaseSearch.php"); include_once(__CA_LIB_DIR__."/ca/Search/ObjectSearchResult.php"); class ObjectSearch extends BaseSearch { # ---------------------------------------------------------------------- /** * Which table does this class represent? */ protected $ops_tablename = "ca_objects"; protected $ops_primary_key = "object_id"; public $user; # ---------------------------------------------------------------------- public function &search($ps_search, $pa_options=null, $user) { return parent::doSearch($ps_search, new ObjectSearchResult(), $pa_options); } # ---------------------------------------------------------------------- } ?>

In the following code I can't pass the $user property to the search method. I tried with $user, $this->user and new ObjectSearch($user). Being new to PHP I know I'm asking a naive question, but I can't solve it by myself, believe me I tried for days. How can I accomplish this?

$po_request = $this->getVar('request'); $vs_widget_id = $this->getVar('widget_id'); $user = $this->getVar('user'); $o_search = new ObjectSearch(); $result = $o_search->search('created.$user.:"2013"'); $count = 1; while($result->nextHit()) { print "Hit ".$count.": "."<br/>\n"; print "Idno: ".$result->get('ca_objects.idno')."<br/>\n"; print "Name: ".$result->get('ca_objects.preferred_labels.name')."<br/>\n"; $count++; } ?>

最满意答案

public function &search($ps_search, $pa_options=null, $user)

它有一些问题:

在具有默认值的参数之后传递没有默认值的参数没有任何意义 你必须在这里传递第三个参数(你只传递一个) 您不必手动传递类属性; 他们自动在$this

所以写:

public function &search($ps_search, $pa_options=null) { return parent::doSearch($ps_search, new ObjectSearchResult($this->user), $pa_options); }

或者您可能需要$user class属性,只需编写$this->user 。

$this始终在对象上下文中设置:您不需要自己传递它。

public function &search($ps_search, $pa_options=null, $user)

There are a few things wrong with it:

It doesn't make any sense to pass a parameter without default value after a parameter which has a default value You have to pass the third parameter here (you only pass one) You don't have to pass the class properties manually; they're automatically in $this

So write:

public function &search($ps_search, $pa_options=null) { return parent::doSearch($ps_search, new ObjectSearchResult($this->user), $pa_options); }

Or where ever you may need your $user class property, write simply $this->user.

$this is always set in object context: you don't need to pass it yourself.