Method/function overloading

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

    #1

    Method/function overloading

    Am I correct in thinking there is no method/function overloading of any
    kind in any version of PHP?

    Thanks,

    Matthew
  • Captain Paralytic

    #2
    Re: Method/function overloading

    On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
    Am I correct in thinking there is no method/function overloading of any
    kind in any version of PHP?
    >
    Thanks,
    >
    Matthew
    Depends if any of the about 758,000 items found by putting the words

    overloading php

    into Google fit the bill.

    Comment

    • Matthew

      #3
      Re: Method/function overloading

      Captain Paralytic emailed this:
      On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
      >Am I correct in thinking there is no method/function overloading of any
      >kind in any version of PHP?
      >>
      >Thanks,
      >>
      >Matthew
      >
      Depends if any of the about 758,000 items found by putting the words
      >
      overloading php
      >
      into Google fit the bill.
      I did that before posting, and got hits for class, property, method, and
      an extension for overloading. When I looked in manual I found a function
      called overload() which will enable method and property overloading in PHP
      versions 4 >= 4.2.0.

      I can't seem to find a simple answer to my question, hence my post, which
      is specific to methods/functions (including constructors). I was unable to
      find an example of what I would call 'method overloading' that you would
      use in C++, Java, C#, etc..

      EG.

      function DoThis()
      {
      //something
      }
      function DoThis($var1)
      {
      //something
      }
      function DoThis($var1, $var2)
      {
      //something
      }

      the method that runs is chosen simply on the basis of the number of arguments.

      Thanks.

      Comment

      • Rik Wasmus

        #4
        Re: Method/function overloading

        On Mon, 10 Dec 2007 15:17:56 +0100, Matthew <matthew@spamki ller.comwrote:
        Am I correct in thinking there is no method/function overloading of any
        kind in any version of PHP?
        RTFM:

        --
        Rik Wasmus

        Comment

        • Captain Paralytic

          #5
          Re: Method/function overloading

          On 10 Dec, 14:56, Matthew <matt...@spamki ller.comwrote:
          Captain Paralytic emailed this:
          >
          On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
          Am I correct in thinking there is no method/function overloading of any
          kind in any version of PHP?
          >
          Thanks,
          >
          Matthew
          >
          Depends if any of the about 758,000 items found by putting the words
          >
          overloading php
          >
          into Google fit the bill.
          >
          I did that before posting, and got hits for class, property, method, and
          an extension for overloading. When I looked in manual I found a function
          called overload() which will enable method and property overloading in PHP
          versions 4 >= 4.2.0.
          >
          I can't seem to find a simple answer to my question, hence my post,
          Your question was whether you were right in "thinking there is no
          method/function overloading of any kind in any version of PHP"

          You have now said that "I found a function called overload() which
          will enable method and property overloading"

          So it seems pretty obvious to me that you already had the answer to
          the question you asked.

          Comment

          • Jerry Stuckle

            #6
            Re: Method/function overloading

            Matthew wrote:
            Am I correct in thinking there is no method/function overloading of any
            kind in any version of PHP?
            >
            Thanks,
            >
            Matthew
            >
            Not in the way I think you're saying - where two methods in the same
            class (or both not in any class) can have the same name but different
            parameter lists . PHP hasn't gotten that far yet. Maybe in a future
            release.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • ZeldorBlat

              #7
              Re: Method/function overloading

              On Dec 10, 9:56 am, Matthew <matt...@spamki ller.comwrote:
              Captain Paralytic emailed this:
              >
              On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
              Am I correct in thinking there is no method/function overloading of any
              kind in any version of PHP?
              >
              Thanks,
              >
              Matthew
              >
              Depends if any of the about 758,000 items found by putting the words
              >
              overloading php
              >
              into Google fit the bill.
              >
              I did that before posting, and got hits for class, property, method, and
              an extension for overloading. When I looked in manual I found a function
              called overload() which will enable method and property overloading in PHP
              versions 4 >= 4.2.0.
              >
              I can't seem to find a simple answer to my question, hence my post, which
              is specific to methods/functions (including constructors). I was unable to
              find an example of what I would call 'method overloading' that you would
              use in C++, Java, C#, etc..
              >
              EG.
              >
              function DoThis()
              {
              //something}
              >
              function DoThis($var1)
              {
              //something}
              >
              function DoThis($var1, $var2)
              {
              //something
              >
              }
              >
              the method that runs is chosen simply on the basis of the number of arguments.
              >
              Thanks.
              You can achieve the same thing by using default values for function
              parameters:

              function DoThis($var1 = 'foo', $var2 = 'bar')

              If you don't provide values for those parameters it will take the
              default instead.

              Comment

              • Matthew

                #8
                Re: Method/function overloading

                Jerry Stuckle emailed this:
                Matthew wrote:
                >Am I correct in thinking there is no method/function overloading of
                >any kind in any version of PHP?
                >>
                >Thanks,
                >>
                >Matthew
                >>
                >
                Not in the way I think you're saying - where two methods in the same
                class (or both not in any class) can have the same name but different
                parameter lists . PHP hasn't gotten that far yet. Maybe in a future
                release.
                Thanks Jerry - this is what I meant and wanted to know.
                Cheers.

                Comment

                • Matthew

                  #9
                  Re: Method/function overloading

                  ZeldorBlat emailed this:
                  On Dec 10, 9:56 am, Matthew <matt...@spamki ller.comwrote:
                  >Captain Paralytic emailed this:
                  >>
                  >>On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
                  >>>Am I correct in thinking there is no method/function overloading of any
                  >>>kind in any version of PHP?
                  >>>Thanks,
                  >>>Matthew
                  >>Depends if any of the about 758,000 items found by putting the words
                  >>overloading php
                  >>into Google fit the bill.
                  >I did that before posting, and got hits for class, property, method, and
                  >an extension for overloading. When I looked in manual I found a function
                  >called overload() which will enable method and property overloading in PHP
                  >versions 4 >= 4.2.0.
                  >>
                  >I can't seem to find a simple answer to my question, hence my post, which
                  >is specific to methods/functions (including constructors). I was unable to
                  >find an example of what I would call 'method overloading' that you would
                  >use in C++, Java, C#, etc..
                  >>
                  >EG.
                  >>
                  >function DoThis()
                  >{
                  > //something}
                  >>
                  >function DoThis($var1)
                  >{
                  > //something}
                  >>
                  >function DoThis($var1, $var2)
                  >{
                  > //something
                  >>
                  >}
                  >>
                  >the method that runs is chosen simply on the basis of the number of arguments.
                  >>
                  >Thanks.
                  >
                  You can achieve the same thing by using default values for function
                  parameters:
                  >
                  function DoThis($var1 = 'foo', $var2 = 'bar')
                  >
                  If you don't provide values for those parameters it will take the
                  default instead.
                  It's not the same thing. But thanks for the hint.
                  Cheers.

                  Comment

                  • ZeldorBlat

                    #10
                    Re: Method/function overloading

                    On Dec 10, 11:40 am, Matthew <matt...@spamki ller.comwrote:
                    ZeldorBlat emailed this:
                    >
                    >
                    >
                    On Dec 10, 9:56 am, Matthew <matt...@spamki ller.comwrote:
                    Captain Paralytic emailed this:
                    >
                    >On 10 Dec, 14:17, Matthew <matt...@spamki ller.comwrote:
                    >>Am I correct in thinking there is no method/function overloading of any
                    >>kind in any version of PHP?
                    >>Thanks,
                    >>Matthew
                    >Depends if any of the about 758,000 items found by putting the words
                    >overloading php
                    >into Google fit the bill.
                    I did that before posting, and got hits for class, property, method, and
                    an extension for overloading. When I looked in manual I found a function
                    called overload() which will enable method and property overloading in PHP
                    versions 4 >= 4.2.0.
                    >
                    I can't seem to find a simple answer to my question, hence my post, which
                    is specific to methods/functions (including constructors). I was unable to
                    find an example of what I would call 'method overloading' that you would
                    use in C++, Java, C#, etc..
                    >
                    EG.
                    >
                    function DoThis()
                    {
                    //something}
                    >
                    function DoThis($var1)
                    {
                    //something}
                    >
                    function DoThis($var1, $var2)
                    {
                    //something
                    >
                    }
                    >
                    the method that runs is chosen simply on the basis of the number of arguments.
                    >
                    Thanks.
                    >
                    You can achieve the same thing by using default values for function
                    parameters:
                    >
                    function DoThis($var1 = 'foo', $var2 = 'bar')
                    >
                    If you don't provide values for those parameters it will take the
                    default instead.
                    >
                    It's not the same thing. But thanks for the hint.
                    Cheers.
                    I didn't say it was the same. But you can achieve the same effect.
                    I've seen plenty of constructs like this:

                    function DoThis() {
                    return DoThis('foo');
                    }

                    function DoThis($var1) {
                    return DoThis($var1, 'bar');
                    }

                    functionDoThis( $var1, $var2) {
                    //do something
                    }


                    In PHP the equivalent is:

                    function DoThis($var1 = 'foo', $var2 = 'bar') {
                    //do something
                    }

                    Comment

                    • Toby A Inkster

                      #11
                      Re: Method/function overloading

                      Matthew wrote:
                      Am I correct in thinking there is no method/function overloading of any
                      kind in any version of PHP?
                      OK. In some other programming language you might have:

                      function reverse (int $i)
                      { return 0 - $i; }

                      function reverse (string $s)
                      { return strrev($s); }

                      function reverse (array $a)
                      { return array_reverse($ a); }

                      In PHP you'd write:

                      function _reverse_int ($i)
                      { return 0 - $i; }

                      function _reverse_string ($s)
                      { return strrev($s); }

                      function _reverse_array ($a)
                      { return array_reverse($ a); }

                      function reverse ($x)
                      {
                      if (is_numeric($x) )
                      return _reverse_int($x );
                      elseif (is_string($x))
                      return _reverse_string ($x);
                      elseif (is_array($x))
                      return _reverse_array( $x);
                      else
                      {
                      $emsg = sprintf("Can't reverse type '%s'",
                      gettype($x));
                      throw new Exception($emsg );
                      }
                      }

                      Different syntax, same effect.

                      Also, with regards to the number of arguments, consider:

                      function add_ints (int $a, int $b)
                      { return $a + $b; }

                      function add_ints (int $a, int $b, int $c)
                      { return $a + $b + $c; }

                      In PHP you could use a similar technique to what I showed before:

                      function _add_ints__2 ($a, $b)
                      { return $a + $b; }

                      function _add_ints__3 ($a, $b, $c)
                      { return $a + $b + $c; }

                      function add_ints ()
                      {
                      if (func_num_args( )==2)
                      return call_user_func_ array('_add_int s__2',
                      func_get_args() );
                      elseif (func_num_args( )==3)
                      return call_user_func_ array('_add_int s__3',
                      func_get_args() );
                      }

                      But it would be simpler to rewrite the function as:

                      function add_ints ($a=0, $b=0, $c=0)
                      {
                      return $a + $b + $c;
                      }

                      Or:

                      function add_ints ()
                      {
                      return array_reduce(
                      func_get_args() ,
                      create_function ('$a,$b', 'return $a+$b;')
                      );
                      }

                      --
                      Toby A Inkster BSc (Hons) ARCS
                      [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
                      [OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 3 days, 22:33.]

                      Sharing Music with Apple iTunes

                      Comment

                      Working...