Overloaded functions and function pointers in PHP?

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

    #1

    Overloaded functions and function pointers in PHP?

    Hi all,

    Since PHP is a (weakly) typed language, is it possible to overload functions
    based on the type of their argument?

    Also, are there function pointers in PHP?


    Here's why I'm asking. Consider these two function:


    function foo( $arg ) { echo "foo $arg foo\n"; }

    function bar() { echo "barfoo"; }


    I'd like to be able to do this:

    foo('hello');
    foo(&bar());

    to produce this:

    foo hello foo
    foo barfoo foo

    Is this at all possible (and convenient!) with PHP?

    Thanks!
    Pete
  • Oli Filth

    #2
    Re: Overloaded functions and function pointers in PHP?

    Peter Salzman said the following on 10/09/2005 17:21:[color=blue]
    > Hi all,
    >
    > Since PHP is a (weakly) typed language, is it possible to overload functions
    > based on the type of their argument?[/color]

    No. But there are default arguments, and variable-length argument lists
    (PHP 4+).
    See http://www.php.net/manual/functions.arguments.php.

    [color=blue]
    > Also, are there function pointers in PHP?[/color]

    No. But there are functions like call_user_func( ), which make them
    somewhat redundant.
    See http://www.php.net/call_user_func.
    [color=blue]
    >
    > Here's why I'm asking. Consider these two function:
    >
    >
    > function foo( $arg ) { echo "foo $arg foo\n"; }
    >
    > function bar() { echo "barfoo"; }
    >
    >
    > I'd like to be able to do this:
    >
    > foo('hello');
    > foo(&bar());
    >
    > to produce this:
    >
    > foo hello foo
    > foo barfoo foo
    >[/color]

    How about:

    function foo($arg)
    {
    echo "foo $arg foo\n";
    }

    function bar()
    {
    return "barfoo";
    }

    foo(bar());


    --
    Oli

    Comment

    • Peter Salzman

      #3
      Re: Overloaded functions and function pointers in PHP?

      Oli Filth <catch@olifilth .co.uk> wrote:[color=blue]
      > Peter Salzman said the following on 10/09/2005 17:21:[color=green]
      > >
      > > Here's why I'm asking. Consider these two function:
      > >
      > >
      > > function foo( $arg ) { echo "foo $arg foo\n"; }
      > >
      > > function bar() { echo "barfoo"; }
      > >
      > >
      > > I'd like to be able to do this:
      > >
      > > foo('hello');
      > > foo(&bar());
      > >
      > > to produce this:
      > >
      > > foo hello foo
      > > foo barfoo foo
      > >[/color]
      >
      > How about:
      >
      > function foo($arg)
      > {
      > echo "foo $arg foo\n";
      > }
      >
      > function bar()
      > {
      > return "barfoo";
      > }
      >
      > foo(bar());[/color]


      The function argument seems to be executing before the call to foo is made.
      In my own code, I'm getting the equivalent of:

      barfoo foo foo

      rather than

      foo barfoo foo


      Pete

      Comment

      • Oli Filth

        #4
        Re: Overloaded functions and function pointers in PHP?

        Peter Salzman said the following on 10/09/2005 17:45:[color=blue]
        > Oli Filth <catch@olifilth .co.uk> wrote:
        >[color=green]
        >>Peter Salzman said the following on 10/09/2005 17:21:
        >>[color=darkred]
        >>>Here's why I'm asking. Consider these two function:
        >>>
        >>>
        >>> function foo( $arg ) { echo "foo $arg foo\n"; }
        >>>
        >>> function bar() { echo "barfoo"; }
        >>>
        >>>
        >>>I'd like to be able to do this:
        >>>
        >>> foo('hello');
        >>> foo(&bar());
        >>>
        >>>to produce this:
        >>>
        >>> foo hello foo
        >>> foo barfoo foo
        >>>[/color]
        >>
        >>How about:
        >>
        >> function foo($arg)
        >> {
        >> echo "foo $arg foo\n";
        >> }
        >>
        >> function bar()
        >> {
        >> return "barfoo";
        >> }
        >>
        >> foo(bar());[/color]
        >
        >
        >
        > The function argument seems to be executing before the call to foo is made.
        > In my own code, I'm getting the equivalent of:
        >
        > barfoo foo foo
        >
        > rather than
        >
        > foo barfoo foo
        >[/color]

        That's because your bar() is echoing; mine is returning.

        --
        Oli

        Comment

        • Chung Leong

          #5
          Re: Overloaded functions and function pointers in PHP?

          Peter Salzman wrote:[color=blue]
          > Since PHP is a (weakly) typed language, is it possible to overload functions
          > based on the type of their argument?[/color]

          Kind of. You can always check the type of argument that's actually
          passed with one of the is_* function and have the function behave
          differently (e.g. array vs. scalar).
          [color=blue]
          > Also, are there function pointers in PHP?[/color]

          Kind of. In PHP, functions are referenced by name. If you do $func()
          and the variable $func contains the name of a function, it will be
          called.
          [color=blue]
          > I'd like to be able to do this:
          >
          > foo('hello');
          > foo(&bar());
          >
          > to produce this:
          >
          > foo hello foo
          > foo barfoo foo[/color]

          I don't think that's doable in any language.

          Comment

          Working...