0

我是 OOP 的新手,但仍然对这些概念感到困惑

我创建了一个类方法,它将从 Zend_Session_Namespace 中提取两组数据。我现在的问题是,当这些数据被拉入另一种方法时,我不知道如何提取这些数据。

如果我告诉你我的意思可能是最好的:

 Public function rememberLastProductSearched()
          {
    $session = new Zend_Session_Namespace(searchedproducts);

    if ($this->getRequest()->getParam('product-searched')) { 
    $session->ProductSearched = $this->getRequest()->getParam('product-searched');
    return " $session->ProductSearched";
    } else { 
    if ($session->ProductSearched) {
    return " $session->ProductSearched "; 
    }  
    }

    if ($this->getRequest()->getParam('search-term')) { 
    $session->SearchTerm = $this->getRequest()->getParam('search-term');
    return " $session->SearchTerm";
    } else { 
    if ($session->SearchTerm) {
    return " $session->SearchTerm "; 
    }
    }

该方法应获得两组数据,即

$session->SearchTerm 
$session->ProductSearched

我的困惑是这样的;我现在如何在另一个方法调用中提取两组数据(即在同一个类中)。即

以上是我提取信息的尝试-但它没有用。

或者,我是否应该将信息放入一个数组中 - 如果是这样,有人可以告诉我我是如何做到这一点的。

4

2 回答 2

0

行“return $something;” 将停止代码执行并返回值。如果要返回多个值,则需要返回一个数组或使用两个单独的函数来返回这些值。如果你想返回一个数组,你可以这样做:

    public function rememberLastProductSearched() {
          $returnArray = array();

          $session = new Zend_Session_Namespace(searchedproducts);

          if ($this->getRequest()->getParam('product-searched')) { 
            $session->ProductSearched = $this->getRequest()->getParam('product-searched');
            $returnArray['productSearched'] = $session->ProductSearched;
          } else { 
            if ($session->ProductSearched) {
              $returnArray['productSearched'] = $session->ProductSearched; 
            }  
          }

        if ($this->getRequest()->getParam('search-term')) { 
          $session->SearchTerm = $this->getRequest()->getParam('search-term');
          $returnArray['searchTerm'] = $session->SearchTerm;
        } else { 
          if ($session->SearchTerm) {
            $returnArray['searchTerm'] = $session->SearchTerm; 
          }
        }

        return $returnArray;
}

在您的控制器或您想要检查这些值的任何地方:

$lastSearch = $this->rememberLastProductSearched();
echo $lastSearch['productSearched']; // Product Searched
echo $lastSearch['searchTerm'];      // Search terms

但是使用两个功能可能更干净

public function getLastProductSearched() {
              $session = new Zend_Session_Namespace(searchedproducts);

              if ($this->getRequest()->getParam('product-searched')) { 
                $session->ProductSearched = $this->getRequest()->getParam('product-searched');
                $returnValue = $session->ProductSearched;
              } else { 
                if ($session->ProductSearched) {
                  $returnValue = $session->ProductSearched; 
                }  
              }
              return $returnValue;
}

public function getLastSearchTerms() {
              $session = new Zend_Session_Namespace(searchedproducts);

              if ($this->getRequest()->getParam('search-term')) { 
                $session->SearchTerm= $this->getRequest()->getParam('search-term');
                $returnValue = $session->SearchTerm;
              } else { 
                if ($session->SearchTerm) {
                  $returnValue = $session->SearchTerm; 
                }  
              }
              return $returnValue;

}

你可以像这样使用它们:

echo $this->getLastProductSearched(); // Product Searched
echo $this->getLastSearchTerms();      // Search terms

这将使您的代码在以后更易于阅读和调试。关于您的代码的更多注释。您可以通过使用 || 来避免使用嵌套的 if。

if ($this->getRequest()->getParam('product-searched') || $session->ProductSearched) {
  $returnValue = $this->getRequest()->getParam('product-searched') || $session->ProductSearched;
}

将实现相同的目标:

if ($this->getRequest()->getParam('product-searched')) { 
            $session->ProductSearched = $this->getRequest()->getParam('product-searched');
            $returnArray['productSearched'] = $session->ProductSearched;
          } else { 
            if ($session->ProductSearched) {
              $returnArray['productSearched'] = $session->ProductSearched; 
            }  
          }

希望这可以帮助 !

于 2013-06-11T14:05:01.460 回答
0

看起来您正在尝试做的是使用参数中的product-searchedandsearch-terms并将它们存储在会话中(如果已设置),否则访问以前保存的值。看看您如何调用此方法会有所帮助,但我可能会稍微修改您的代码以返回会话命名空间对象,因为它包含两个值,无论它们来自 params 还是已经存在:

public function rememberLastProductSearched()
{
    $searchedProducts = new Zend_Session_Namespace('searchedproducts');

    if ($this->getRequest()->getParam('product-searched')) { 
        $searchedProducts->ProductSearched = $this->getRequest()->getParam('product-searched');
    }

    if ($this->getRequest()->getParam('search-term')) { 
        $searchedProducts->SearchTerm = $this->getRequest()->getParam('search-term');
    }

    return $searchedProducts;
}

我假设你在控制器类中有这个方法,所以你会这样称呼它:

public function searchAction()
{
    $searchedProducts = $this->rememberLastProductSearched();

    // do something with the values here
}

然后,您将在$searchedProducts->ProductSearched和中获得这两个值$searchedProducts->SearchTerm

于 2013-06-11T13:51:03.663 回答