将属性传递给PHP方法

时间:2013-05-22 19:47:11

标签: php oop methods properties

我有以下课程,我在其中添加了属性$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->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++;

}


 ?>

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

它有一些问题:

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

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

    或者您可能需要$user课程属性,只需撰写$this->user

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

相关问题