从方法类中提取两组数据的正确方法是什么

时间:2013-06-11 13:31:54

标签: oop zend-framework

我是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

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

以上是我尝试提取信息 - 但它不起作用。

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

2 个答案:

答案 0 :(得分:0)

您尝试执行的操作似乎是使用params中的product-searchedsearch-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中获得两个值。

答案 1 :(得分:0)

该行“返回$ 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

它将使您的代码以后更容易阅读和调试。关于代码的一些注释。您可以通过使用||。

来避免使用嵌套ifs
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; 
            }  
          }

希望这有帮助!