$class->method()->method()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • edg@greenberg.org

    $class->method()->method()

    This works:

    $b = $a->method1();
    $c = $b->method2();

    I'd like to get this all on one expression, something like:

    $c = ${$a->method1()}->method2();
    which doesn't work.

    Is there any way to avoid actually creating and then using the $b
    object?

    Thanks,
    </edg>
    Ed Greenberg
    San Jose CA USA

  • Rutger Claes

    #2
    Re: $class-&gt;method()-&gt;method()

    edg@greenberg.o rg wrote:
    [color=blue]
    > This works:
    >
    > $b = $a->method1();
    > $c = $b->method2();
    >
    > I'd like to get this all on one expression, something like:
    >
    > $c = ${$a->method1()}->method2();
    > which doesn't work.
    >
    > Is there any way to avoid actually creating and then using the $b
    > object?
    >
    > Thanks,
    > </edg>
    > Ed Greenberg
    > San Jose CA USA[/color]

    If I'm not mistaking you'll be needing PHP5 to use that syntax...
    In PHP5 $a->method1()->method2() works, in PHP4 it doesn't as far as I know.

    Rutger
    --
    Rutger Claes rgc@rgc.tld
    Replace tld with top level domain of belgium to contact me pgp:0x3B7D6BD6
    Do not reply to the from address. It's read by /dev/null and sa-learn only

    Comment

    • Matthias Esken

      #3
      Re: $class-&gt;method()-&gt;method()

      edg@greenberg.o rg wrote:
      [color=blue]
      > This works:
      >
      > $b = $a->method1();
      > $c = $b->method2();
      >
      > I'd like to get this all on one expression, something like:
      >
      > $c = ${$a->method1()}->method2();
      > which doesn't work.[/color]

      Use $c = $a->method1()->method2;

      You'll need PHP 5 for this syntax.

      Regards,
      Matthias

      Comment

      • Andy Hassall

        #4
        Re: $class-&gt;method()-&gt;method()

        On 6 Jan 2005 09:41:24 -0800, edg@greenberg.o rg wrote:
        [color=blue]
        >This works:
        >
        >$b = $a->method1();
        >$c = $b->method2();
        >
        >I'd like to get this all on one expression, something like:
        >
        >$c = ${$a->method1()}->method2();
        >which doesn't work.
        >
        >Is there any way to avoid actually creating and then using the $b
        >object?[/color]

        andyh@server:~/public_html$ cat test.php
        <?php
        class B {
        function method2()
        {
        return 'x';
        }
        }

        class A {
        function method1()
        {
        return new B();
        }
        }

        $a = new A();
        $c = $a->method1()->method2();

        print $c;
        print "\n";
        ?>

        andyh@server:~/public_html$ php -q test.php
        x

        andyh@server:~/public_html$ php -v
        PHP 5.0.3 (cli) (built: Dec 22 2004 02:41:40)
        Copyright (c) 1997-2004 The PHP Group
        Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies

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

        Comment

        • edg@greenberg.org

          #5
          Re: $class-&gt;method()-&gt;method()

          Thanks for all the responses.

          I suppose I should have posted that I have to use PHP4 :(
          Any ideas for the more traditional approach?

          Comment

          Working...