Issues

ZF-80: Zend_Controller_Action::_getParam default return value logic error (TRAC#90 ralph)

Description

http://framework.zend.com/developer/ticket/90

Logically, if no param by the expected name is passed to the controller, the default return valued from _getParam should be NULL not FALSE.

More notes here:

http://zend.com/lists/fw-general/…

...

a better demonstration:


http://localhost/controller/some/

public function someAction()
{

   $id = $this->_getParam("id");

   if (!is_null($id))
        echo "I am not null\n";

   if (isset($id))
        echo "I am set\n";

}

produces

I am not null I am set

When indeed id was not set and should be null.

proposed change to _getParam():


    /**
     * Gets a parameter that was passed to this controller.  If the
     * parameter does not exist, NULL will be return.
     *
     * If the parameter does not exist and $default is set, then
     * $default will be returned instead of FALSE.
     *
     * @param string $paramName
     * @param string $default
     * @return mixed
     */
    final protected function _getParam($paramName, $default=null)
    {
        if (array_key_exists($paramName, $this->_params)) {
            return $this->_params[$paramName];
        }

        return $default;
    }

Comments

05/26/06 18:12:31: Modified by gavin

* owner changed from zend to jayson.
* priority changed from major to minor.

I do see value in allowing ZF applications to distinguish between: 1) parameters that were not provided in the URL, 2) parameters that were provided in the URL, but had no value specified in the URL, and 3) parameters that were provided in the URL along with a value (at least 1 character).

Regardless of opinions about best practices and how an application was designed, all 3 of these cases can and do occur. 05/27/06 12:21:56: Modified by ralph@smashlabs.com

Indeed, to take care of 1) and 2) it sounds as if a method likely named _hasParam() of return boolean would suffice. And I agree on point three. Lets not be confused about what a character is though (in the php everything is a string sense of the word ;) ). If the controller sees /id// in the url, it should assume that id has no value, or is NULL. On the otherhand, if it sees /id/%20/, id would then have the value of " "... a string of length 1.

According to what you said, lets consider the following code:

if ($this->_hasParam("id")) $id = $this->_getParam("id");

-> http://localhost/controller/action/id/%20/ _hasParam would return true, _getParam would return " ".

-> http://localhost/controller/action/id// _hasParam would return true, _getParam would return NULL;

-> http://localhost/controller/action/ _hasParam would return false

ps. i agree that this is a minor issue, but if possible it would be nice to see this implemented in 0.1.4 05/28/06 20:52:53: Modified by bell@creativenerd.com

Why NULL, why not make this return an empty string? Then you eliminate the need for _hasParam()

-> http://localhost/controller/action/id// _hasParam would return true, _getParam would return NULL;

05/29/06 01:43:47: Modified by ralph@smashlabs.com

An empty string is not null NULL, an empty string implies a variable is set, which the url http://localhost/controller/action/id// seems to imply otherwise. That url string lexically says to me "$id = null;". Essentially, this comes down to expected behavior when used in any of the contexts listed here: http://us2.php.net/manual/en/types.comparisons.php

Furthermore, the very definition of null seems best to suit this senerio http://us2.php.net/manual/en/… "it has not been set to any value"

-ralph 06/12/06 06:04:53: Modified by URL dkny watches timberland boots on sale breitling watches adipex phentermine corum watches harley davidson womens boots below the knee boots personalized luggage tag used rolex watches steel toe boots chippewa boots croton watches womens shoes wide width sketchers shoes dunham hiking boots watches swiss army fendi sunglasses mature women in pantyhose and silk stockings volcom purses women motorcycle boots 06/14/06 17:44:28: Modified by gavin

Although not exactly the same issue, the final solution for both Zend_Controller_Action and Zend_Input_Filter should have consistent semantics. For example, null indicates parameter not available/given, an empty string indicates the parameter was supplied / is available, and a non-empty string would contain the value of the parameter.

References:

* <a rel="nofollow" href="http://www.zend.com/lists/fw-general/200604/msg00380.html">http://zend.com/lists/fw-general/&hellip;</a>
* #142 

The MVC implementation in 0.2.0 (incubator; also current SVN) implemented this as the Request object was designed to return null when a value was missing or a key does not exist.

In revision 1469, added _hasParam() per the commentary to this issue.