PHP 模块 - 变量不会设置

发布于 2024-12-18 14:52:10 字数 1659 浏览 0 评论 0原文

我对模块化编程很陌生。

我在模块中设置变量时遇到问题,但仅限于特定函数。

我有(删除了无用的东西):

class Products extends Modules {

  private $resultsFound;

  function __construct() {
    parent::__construct();
  }

  public function getResultsFound() {
    return $this->resultsFound;
  }

  private function setResultsFound($resultsFound) {
    $this->resultsFound = $resultsFound;
  }

}

我在模块中有 2 个公共函数,两者都做几乎相同的事情,但其中一个将使用 $this->setResultsFound(12) 和一个设置 var惯于。

public function sortSearchBar($categoryID, $brandID, $sort = false, $limit = false, $search = false){
    foreach ($this->sortAwway as $key => $val) {
        $optionItems[] = '<option value="'.$key.'"'. (($sort == $key) ? ' selected="selected"' : '' ) .'>'.$this->htmlspecialchars($val).'</option>';
    }

    foreach ($this->searchLimit as $key => $val) {
        $limitItems[] = '<option value="'.$key.'"'. (($limit == $key) ? ' selected="selected"' : '' ) .'>'.$this->htmlspecialchars($val).'</option>';
    }
    $this->setResultsFound(12); //works

    return '
    <form action=...
    </form>';
}

public function showProductItemList($categoryID, $brandID = false, $page, $sort = false, $limit = false, $search = false, $cleanURL = true){

    //echo $this->echoArray($this->getProductsForCategory($categoryID, $brandID));
    $q = $this->getProductsForCategory($categoryID, $brandID, $sort, $search);

    $this->setResultsFound(12); //doesn't work

    return $this->formatProductResults($q, $limit, $cleanURL, $page);
}

有谁知道为什么吗?

干杯, 里斯

I'm pretty newish to modular programming.

I'm having trouble setting a variable in a module, but only in particular functions.

I have (useless stuff removed):

class Products extends Modules {

  private $resultsFound;

  function __construct() {
    parent::__construct();
  }

  public function getResultsFound() {
    return $this->resultsFound;
  }

  private function setResultsFound($resultsFound) {
    $this->resultsFound = $resultsFound;
  }

}

I have 2 public functions in the module, both do near enough the same thing, but one will set the var with $this->setResultsFound(12) and one won't.

public function sortSearchBar($categoryID, $brandID, $sort = false, $limit = false, $search = false){
    foreach ($this->sortAwway as $key => $val) {
        $optionItems[] = '<option value="'.$key.'"'. (($sort == $key) ? ' selected="selected"' : '' ) .'>'.$this->htmlspecialchars($val).'</option>';
    }

    foreach ($this->searchLimit as $key => $val) {
        $limitItems[] = '<option value="'.$key.'"'. (($limit == $key) ? ' selected="selected"' : '' ) .'>'.$this->htmlspecialchars($val).'</option>';
    }
    $this->setResultsFound(12); //works

    return '
    <form action=...
    </form>';
}

public function showProductItemList($categoryID, $brandID = false, $page, $sort = false, $limit = false, $search = false, $cleanURL = true){

    //echo $this->echoArray($this->getProductsForCategory($categoryID, $brandID));
    $q = $this->getProductsForCategory($categoryID, $brandID, $sort, $search);

    $this->setResultsFound(12); //doesn't work

    return $this->formatProductResults($q, $limit, $cleanURL, $page);
}

Does anyone have any idea why?

Cheers,
Rhys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2024-12-25 14:52:10

从字里行间看,我认为 setResultsFound() 方法可能应该被声明为 protected,而不是 private

阅读本文

Reading between the lines, I think that the setResultsFound() method should probably be declared protected, not private.

Read this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文