References in PHP5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • [Mystic]

    #1

    References in PHP5

    Hi

    I was just wondeirng how they work now, esspically regarding functions and
    return values.

    Do functions pass return by value or reference?

    Concidering the following:

    public static function &getInstane( ) {
    static $database_objec t; // reference object

    if (!is_object($da tabase))
    $database_objec t = new database(); // create new object

    return $database_objec t; // return address of object
    }

    Do I need to return $database_objec t; or return &$database_obje ct;

    or

    When calling the function:

    $test = &database::getI nstane()
    or
    $test = database::getIn stane()

    Thanks


  • Rutger Claes

    #2
    Re: References in PHP5

    "[Mystic]" <screwuspamme r> wrote:
    [color=blue]
    > Hi
    >
    > I was just wondeirng how they work now, esspically regarding functions and
    > return values.
    >
    > Do functions pass return by value or reference?
    >
    > Concidering the following:
    >
    > public static function &getInstane( ) {
    > static $database_objec t; // reference object
    >
    > if (!is_object($da tabase))
    > $database_objec t = new database(); // create new object
    >
    > return $database_objec t; // return address of object
    > }
    >
    > Do I need to return $database_objec t; or return &$database_obje ct;
    >
    > or
    >
    > When calling the function:
    >
    > $test = &database::getI nstane()
    > or
    > $test = database::getIn stane()
    >
    > Thanks[/color]

    PHP 5 passes by reference. As far as I know you don't need to use the &
    anymore.

    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

    • Mark

      #3
      Re: References in PHP5

      "[Mystic]" <screwuspamme r> wrote:
      [color=blue]
      > Hi
      >
      > I was just wondeirng how they work now, esspically regarding functions and
      > return values.
      >
      > Do functions pass return by value or reference?
      >
      > Concidering the following:
      >
      > public static function &getInstane( ) {
      > static $database_objec t; // reference object
      >
      > if (!is_object($da tabase))
      > $database_objec t = new database(); // create new object
      >
      > return $database_objec t; // return address of object
      > }[/color]

      all objects in PHP5 are passed by reference (well, sort of -- only a copy
      of the handle to the object is made, so you're still working on the same
      underlying object), so you do not need to use the & at all.

      as for other types, such as ints or bools, then you will still need to use
      the TWO &s and do:

      function &fun_name()
      {
      static $spooge;
      return $spooge;
      }

      $spludge = &fun_name();


      domo,
      mark.


      --
      I am not an ANGRY man. Remove the rage from my email to reply.

      Comment

      Working...