Returning a value and a status: Best ways?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FLEB

    Returning a value and a status: Best ways?

    I have a function which should convey two things upon its completion.
    First, the return value of the function, consisting of some text. Second,
    it should convey an "error status", which, could be either a simple
    "success", or a number of levels of failure.

    (For the record, it's a logon script indicating success/failure, but I'll
    probably use this elsewhere.)

    The way I see it, I have a couple options, maybe more:

    1.) Return an array. The first element is the return value, the second is
    the status code.

    - Advantages: No global variables used. No "status check" functions.
    - Disadvantages: Can't use a simple "if (function())" call to determine
    success/failure, since it will always return "true". Need to deal with a
    structure (array) instead of a simple variable.

    2.) Return the string on success, or false on failure. Set a global
    variable to the exit status, and make a function to return that status if
    needed (like the "mysql_erro r()" function).

    - Advantages: Can do a simple ===false test to determine success/failure.
    In the case of a failure, simply use the "_error()" function to determine
    what went wrong.
    - Disadvantages: Another function to document and remember. Uses global
    variables.

    I'm leaning toward option 2.) (error lookup function), but I'm just
    wondering: which is the "generally accepted PHP way of doing things" in a
    situation like this?

    Also, is there a generic term for those sort of "error lookup function"s?
    I'm just a programming hobbyist, so I'm not up on *all* the lingo.

    Thanks!

    --
    -- Rudy Fleminger
    -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
    (put "Hey!" in the Subject line for priority processing!)
    -- http://www.pixelsaredead.com
  • Tim Van Wassenhove

    #2
    Re: Returning a value and a status: Best ways?

    In article <adg71lcs1vyp$. uflt4w0gwcqg$.d lg@40tude.net>, FLEB wrote:[color=blue]
    > I have a function which should convey two things upon its completion.
    > First, the return value of the function, consisting of some text. Second,
    > it should convey an "error status", which, could be either a simple
    > "success", or a number of levels of failure.[/color]
    [color=blue]
    > 1.) Return an array. The first element is the return value, the second is
    > the status code.
    > 2.) Return the string on success, or false on failure. Set a global
    > variable to the exit status, and make a function to return that status if
    > needed (like the "mysql_erro r()" function).[/color]

    3) Pass the variables that are going to be changed by the function as
    reference parameters and return the result.


    --

    Comment

    • Chung Leong

      #3
      Re: Returning a value and a status: Best ways?


      "Tim Van Wassenhove" <euki@pi.be> wrote in message
      news:2glvnlF4b0 a6U3@uni-berlin.de...[color=blue]
      > In article <adg71lcs1vyp$. uflt4w0gwcqg$.d lg@40tude.net>, FLEB wrote:[color=green]
      > > I have a function which should convey two things upon its completion.
      > > First, the return value of the function, consisting of some text.[/color][/color]
      Second,[color=blue][color=green]
      > > it should convey an "error status", which, could be either a simple
      > > "success", or a number of levels of failure.[/color]
      >[color=green]
      > > 1.) Return an array. The first element is the return value, the second[/color][/color]
      is[color=blue][color=green]
      > > the status code.
      > > 2.) Return the string on success, or false on failure. Set a global
      > > variable to the exit status, and make a function to return that status[/color][/color]
      if[color=blue][color=green]
      > > needed (like the "mysql_erro r()" function).[/color]
      >
      > 3) Pass the variables that are going to be changed by the function as
      > reference parameters and return the result.[/color]

      4) Return an int on failure and use is_string() in the if statement.


      Comment

      • Andy Hassall

        #4
        Re: Returning a value and a status: Best ways?

        On Sat, 15 May 2004 08:50:11 -0400, "Chung Leong" <chernyshevsky@ hotmail.com>
        wrote:
        [color=blue]
        >
        >"Tim Van Wassenhove" <euki@pi.be> wrote in message
        >news:2glvnlF4b 0a6U3@uni-berlin.de...[color=green]
        >> In article <adg71lcs1vyp$. uflt4w0gwcqg$.d lg@40tude.net>, FLEB wrote:[color=darkred]
        >> > I have a function which should convey two things upon its completion.
        >> > First, the return value of the function, consisting of some text.[/color][/color]
        >Second,[color=green][color=darkred]
        >> > it should convey an "error status", which, could be either a simple
        >> > "success", or a number of levels of failure.[/color]
        >>[color=darkred]
        >> > 1.) Return an array. The first element is the return value, the second[/color][/color]
        >is[color=green][color=darkred]
        >> > the status code.
        >> > 2.) Return the string on success, or false on failure. Set a global
        >> > variable to the exit status, and make a function to return that status[/color][/color]
        >if[color=green][color=darkred]
        >> > needed (like the "mysql_erro r()" function).[/color]
        >>
        >> 3) Pass the variables that are going to be changed by the function as
        >> reference parameters and return the result.[/color]
        >
        >4) Return an int on failure and use is_string() in the if statement.[/color]

        5) (PHP5) Raise an exception.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        Working...