PHP 模块 - 变量不会设置
我对模块化编程很陌生。
我在模块中设置变量时遇到问题,但仅限于特定函数。
我有(删除了无用的东西):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从字里行间看,我认为
setResultsFound()
方法可能应该被声明为protected
,而不是private
。阅读本文。
Reading between the lines, I think that the
setResultsFound()
method should probably be declaredprotected
, notprivate
.Read this.