Referencing returned objects directly

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

    Referencing returned objects directly

    Hi all,

    Is it possible to directly reference an object that is returned by a
    function in PHP?

    ie this:
    echo ReturnAnObject( )->SomeMember;

    instead of this:
    var $Object = ReturnAnObject( );
    echo $Object->SomeMember;

    'cus I can't work out the syntax if it is. :-(

    Cheers,
    Lister

  • ZeldorBlat

    #2
    Re: Referencing returned objects directly

    On Mar 19, 10:59 am, "lister" <listerofsme... @hotmail.comwro te:
    Hi all,
    >
    Is it possible to directly reference an object that is returned by a
    function in PHP?
    >
    ie this:
    echo ReturnAnObject( )->SomeMember;
    >
    instead of this:
    var $Object = ReturnAnObject( );
    echo $Object->SomeMember;
    >
    'cus I can't work out the syntax if it is. :-(
    >
    Cheers,
    Lister
    This code works fine for me:

    <?php
    class foo {
    public $bar = 'bar';
    }

    function baz() {
    return new foo();
    }

    echo baz()->bar;
    ?>

    Comment

    • Jeff

      #3
      Re: Referencing returned objects directly

      On Mar 19, 12:45 pm, "ZeldorBlat " <zeldorb...@gma il.comwrote:
      On Mar 19, 10:59 am, "lister" <listerofsme... @hotmail.comwro te:
      >
      >
      >
      Hi all,
      >
      Is it possible to directly reference an object that is returned by a
      function in PHP?
      >
      ie this:
      echo ReturnAnObject( )->SomeMember;
      >
      instead of this:
      var $Object = ReturnAnObject( );
      echo $Object->SomeMember;
      >
      'cus I can't work out the syntax if it is. :-(
      >
      Cheers,
      Lister
      >
      This code works fine for me:
      >
      <?php
      class foo {
      public $bar = 'bar';
      >
      }
      >
      function baz() {
      return new foo();
      >
      }
      >
      echo baz()->bar;
      ?>
      I think it only works in PHP 5+ not in PHP 4. WIth PHP 4 you have to
      set it to a variable then use that variable to call the function as
      you have in your OP.

      Comment

      Working...