0

我有以下课程,我在其中添加了 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);
        }
        # ----------------------------------------------------------------------
}
?>

在下面的代码中,我无法将$user属性传递给 search 方法。我试过$user,$this->usernew 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++;

}


 ?>
4

2 回答 2

0

不要将双引号与单引号混淆。您必须在这里使用连接或双重连接:

$result = $o_search->search('created ' . $user . ': 2013');

或者

$result = $o_search->search("created $user: 2013");
于 2013-05-22T19:53:28.320 回答
0
    public function &search($ps_search, $pa_options=null, $user)

它有几个问题:

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

所以写:

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

或者你可能需要你的$user类属性的地方,简单地写$this->user.

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

于 2013-05-22T19:53:35.567 回答