Zend_Controller default return value for getParam

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Controller default return value for getParam

Ralph Schindler

It seems to me that if I am trying to retrieve a value that does not
exist in the url, the default action/controller should return NULL
instead of FALSE.  False implies that the value was set to false, rather
than null which is it.

Using this return value in code will break the following logic:

-- snip snip --

http://localhost/controller/some/

public function someAction()
{

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

    if (isset($id))
        die("I am set");
}

-- snip --

will produce "I am set"

Is this a bug or by design?

My proposed change is below.  And I do not know if this carries through
the rest of the framework, but unless the function is checking for a
boolean response, the default response for some value that has the
absence of existence should be NULL.

as per: http://us3.php.net/manual/en/types.comparisons.php

-ralph














     /**
      * Gets a parameter that was passed to this controller.  If the
      * parameter does not exist, FALSE 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 boolean
      */
     final protected function _getParam($paramName, $default=null)
     {
         if (array_key_exists($paramName, $this->_params)) {
             return $this->_params[$paramName];
         }

         if ($default===null) {
             return false;   // <-- THIS SHOULD RETURN NULL
         } else {
             return $default;
         }
     }
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Controller default return value for getParam

Jeunejean Pierre
I agree on that one, we should be able to set a param value as boolean
(true/false).
meanwhile

if (!$this->_getParam('id')) echo('No id param');

Ralph Schindler wrote:

>
> It seems to me that if I am trying to retrieve a value that does not
> exist in the url, the default action/controller should return NULL
> instead of FALSE.  False implies that the value was set to false,
> rather than null which is it.
>
> Using this return value in code will break the following logic:
>
> -- snip snip --
>
> http://localhost/controller/some/
>
> public function someAction()
> {
>
>    $id = $this->_getParam("id");
>
>    if (isset($id))
>     die("I am set");
> }
>
> -- snip --
>
> will produce "I am set"
>
> Is this a bug or by design?
>
> My proposed change is below.  And I do not know if this carries
> through the rest of the framework, but unless the function is checking
> for a boolean response, the default response for some value that has
> the absence of existence should be NULL.
>
> as per: http://us3.php.net/manual/en/types.comparisons.php
>
> -ralph
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>     /**
>      * Gets a parameter that was passed to this controller.  If the
>      * parameter does not exist, FALSE 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 boolean
>      */
>     final protected function _getParam($paramName, $default=null)
>     {
>         if (array_key_exists($paramName, $this->_params)) {
>             return $this->_params[$paramName];
>         }
>
>         if ($default===null) {
>             return false;   // <-- THIS SHOULD RETURN NULL
>         } else {
>             return $default;
>         }
>     }
>
>


--
sincerely yours,
mit freundlichen Grüßen,
cordialement,

Pierre Jeunejean

A D N E X U S
Interactive Media Agency

Rue Patenier, 38
B- 4000 Liege (bureau)

Wirtzfeld, 3A
B- 4760 Büllingen

GSM +32 (0)478 55 65 86
tel +32 (0)4 225 20 84
Skype user name: jeunejeanpierre
[hidden email]

BE 863.696.611

Reply | Threaded
Open this post in threaded view
|

Re: Zend_Controller default return value for getParam

Dawid Schmidt
In reply to this post by Ralph Schindler
I agree, but this should return $default


   /**
     * Gets a parameter that was passed to this controller.  If the
     * parameter does not exist, FALSE 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 boolean
     */
    final protected function _getParam($paramName, $default=null)
    {
        if (array_key_exists($paramName, $this->_params)) {
            return $this->_params[$paramName];
        }

        return $default;
    }



On 5/25/06, Ralph Schindler <[hidden email]> wrote:

It seems to me that if I am trying to retrieve a value that does not
exist in the url, the default action/controller should return NULL
instead of FALSE.  False implies that the value was set to false, rather
than null which is it.

Using this return value in code will break the following logic:

-- snip snip --

http://localhost/controller/some/

public function someAction()
{

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

    if (isset($id))
        die("I am set");
}

-- snip --

will produce "I am set"

Is this a bug or by design?

My proposed change is below.  And I do not know if this carries through
the rest of the framework, but unless the function is checking for a
boolean response, the default response for some value that has the
absence of existence should be NULL.

as per: http://us3.php.net/manual/en/types.comparisons.php

-ralph














     /**
      * Gets a parameter that was passed to this controller.  If the
      * parameter does not exist, FALSE 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 boolean
      */
     final protected function _getParam($paramName, $default=null)
     {
         if (array_key_exists($paramName, $this->_params)) {
             return $this->_params[$paramName];
         }

         if ($default===null) {
             return false;   // <-- THIS SHOULD RETURN NULL
         } else {
             return $default;
         }
     }


kind regards,
Dawid Schmidt