accessing function arguments

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

    accessing function arguments

    i have the following code:

    function some_function($ argument)
    {
    validate_arg();
    //more code
    }

    function validate_arg()
    {
    //this function validates the argument of some_function()
    }

    is it possible for validate_args() to automatically read the argument of
    some_function() or do i have to note the argument in the brackets in any
    case?

    thx, micha

    --
  • Mike Willbanks

    #2
    Re: accessing function arguments

    micha,
    [color=blue]
    > is it possible for validate_args() to automatically read the argument of
    > some_function() or do i have to note the argument in the brackets in any
    > case?[/color]

    No however what you can do is something like the following:


    function some_function() {
    if (!validate_args (func_get_args( ))) {
    return false;
    }
    //some_function is valid...
    }

    This will send all of the arguments from some_function over to
    validate_args.


    Mike

    Comment

    • Ewoud Dronkert

      #3
      Re: accessing function arguments

      On Thu, 19 May 2005 07:56:25 -0500, Mike Willbanks wrote:[color=blue]
      > if (!validate_args (func_get_args( ))) {[/color]

      From the manual: "This function cannot be used directly as a function
      parameter."


      --
      Firefox Web Browser - Rediscover the web - http://getffox.com/
      Thunderbird E-mail and Newsgroups - http://gettbird.com/

      Comment

      • Mike Willbanks

        #4
        Re: accessing function arguments

        Ewoud Dronkert wrote:[color=blue]
        > On Thu, 19 May 2005 07:56:25 -0500, Mike Willbanks wrote:
        >[color=green]
        >>if (!validate_args (func_get_args( ))) {[/color]
        >
        >
        > From the manual: "This function cannot be used directly as a function
        > parameter."
        >
        >[/color]
        Whoops :)

        then:
        $args = func_get_args() ;
        if (!validate_args ($args)) {

        Comment

        • Chung Leong

          #5
          Re: accessing function arguments

          Weird, that is not mentioned in my copy of the manual and you can use
          func_get_args() as an argument with no problem in PHP 4. Must be
          something new.

          Comment

          • Chung Leong

            #6
            Re: accessing function arguments

            Yes. You can use debug_backtrace () to get the arguments to the calling
            function.

            function validate_arg()
            $trace = debug_backtrace ();
            $args = $trace[1]['args'];
            }

            Comment

            • Ewoud Dronkert

              #7
              Re: accessing function arguments

              On 19 May 2005 09:13:51 -0700, Chung Leong wrote:[color=blue]
              > Weird, that is not mentioned in my copy of the manual and you can use
              > func_get_args() as an argument with no problem in PHP 4. Must be
              > something new.[/color]

              Maybe, I never tried. It's mentioned here: http://php.net/func-get-args


              --
              Firefox Web Browser - Rediscover the web - http://getffox.com/
              Thunderbird E-mail and Newsgroups - http://gettbird.com/

              Comment

              • Chung Leong

                #8
                Re: accessing function arguments

                A user note explains it, kind of:

                "func_get_a rg() does not appear to be allowed to be used as a function
                argument itself within class constructors in PHP 5.0.2 (wonk-ay!!!):"


                Why on earth are they introducing little changes like this?

                Comment

                • Mike Willbanks

                  #9
                  Re: accessing function arguments

                  > A user note explains it, kind of:[color=blue]
                  >
                  > "func_get_a rg() does not appear to be allowed to be used as a function
                  > argument itself within class constructors in PHP 5.0.2 (wonk-ay!!!):"
                  >
                  >
                  > Why on earth are they introducing little changes like this?[/color]

                  I was looking at the manual and it says that it will work as long as it
                  is the first parameter in the function.

                  Mike

                  Comment

                  • Chris Hope

                    #10
                    Re: accessing function arguments

                    Chung Leong wrote:
                    [color=blue]
                    > Yes. You can use debug_backtrace () to get the arguments to the calling
                    > function.
                    >
                    > function validate_arg()
                    > $trace = debug_backtrace ();
                    > $args = $trace[1]['args'];
                    > }[/color]

                    But be aware that debug_backtrace () is only available from PHP 4.3.0 and
                    greater. If you are coding for an old version, or for a wide audience
                    that may be using an old version, then it may not be safe to use this
                    function.

                    --
                    Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com

                    Comment

                    • Chung Leong

                      #11
                      Re: accessing function arguments

                      Seems to be the case too in PHP 4. I guess it makes some sense, as PHP
                      has already started pushing the arguments onto the stack if
                      func_get_args() comes after another argument. Interesting how PHP
                      pushes arguments in from left to right, whereas in C, the they get
                      pushed from right to left.

                      Comment

                      • micha

                        #12
                        Re: accessing function arguments

                        micha <chotiwallah@we b.de> wrote in
                        news:Xns965B92A 7087Fchotiwalla h@204.153.244.1 70:
                        [color=blue]
                        > i have the following code:
                        >
                        > function some_function($ argument)
                        > {
                        > validate_arg();
                        > //more code
                        > }
                        >
                        > function validate_arg()
                        > {
                        > //this function validates the argument of some_function()
                        > }
                        >
                        > is it possible for validate_args() to automatically read the argument
                        > of some_function() or do i have to note the argument in the brackets
                        > in any case?
                        >
                        > thx, micha
                        >[/color]

                        debug_backtrace () is a very good way, because it supplies a lot of other
                        information too to use in my validator.

                        thx, micha

                        --

                        Comment

                        • jerry gitomer

                          #13
                          Re: accessing function arguments

                          micha wrote:[color=blue]
                          > micha <chotiwallah@we b.de> wrote in
                          > news:Xns965B92A 7087Fchotiwalla h@204.153.244.1 70:
                          >
                          >[color=green]
                          >>i have the following code:
                          >>
                          >>function some_function($ argument)
                          >> {
                          >> validate_arg();
                          >> //more code
                          >> }
                          >>
                          >>function validate_arg()
                          >> {
                          >> //this function validates the argument of some_function()
                          >> }
                          >>
                          >>is it possible for validate_args() to automatically read the argument
                          >>of some_function() or do i have to note the argument in the brackets
                          >>in any case?
                          >>
                          >>thx, micha
                          >>[/color]
                          >
                          >
                          > debug_backtrace () is a very good way, because it supplies a lot of other
                          > information too to use in my validator.
                          >
                          > thx, micha
                          >[/color]
                          Micha,
                          You have to specify the argument in the brackets. (Well, I
                          suppose it might be possible to avoid doing so, but why go
                          looking for trouble?)

                          So, your code should look like:
                          function some_function($ argument)
                          {
                          validate_arg($a rgument);
                          //more code
                          }

                          function validate_arg($a rgument)
                          {
                          //this function validates the argument of some_function()
                          }

                          HTH
                          Jerry

                          Comment

                          • chotiwallah

                            #14
                            Re: accessing function arguments

                            yes, of course i can specify the argument in the brackets. call it
                            lazyness not to do so.

                            debug_backtrace () works well.

                            micha

                            Comment

                            • Oli Filth

                              #15
                              Re: accessing function arguments

                              chotiwallah wrote:[color=blue]
                              > yes, of course i can specify the argument in the brackets. call it
                              > lazyness not to do so.
                              >
                              > debug_backtrace () works well.[/color]

                              However, the name of this function implies that is supposed to be used
                              for *debugging* purposes. While obviously you can use it for other
                              means, if you are using debug operations to achieve your script's main
                              functionality, it probably means you are going about things the wrong
                              way.
                              [color=blue]
                              >From a progammatic stance, if you want to pass a bunch of unknown[/color]
                              parameters around, either use func_get_args() , or put them into an
                              array.

                              The OP wants to validate a set (presumably of an unknown size) of
                              parameters with one function, which implies they are all of the same
                              type. In this instance, I would use an array.

                              --
                              Oli

                              Comment

                              Working...