Connecting Tech Pros Worldwide Help | Site Map

References in PHP5

  #1  
Old July 17th, 2005, 11:48 AM
[Mystic]
Guest
 
Posts: n/a
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_object; // reference object

if (!is_object($database))
$database_object = new database(); // create new object

return $database_object; // return address of object
}

Do I need to return $database_object; or return &$database_object;

or

When calling the function:

$test = &database::getInstane()
or
$test = database::getInstane()

Thanks


  #2  
Old July 17th, 2005, 11:48 AM
Rutger Claes
Guest
 
Posts: n/a

re: References in PHP5


"[Mystic]" <screwuspammer> 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_object; // reference object
>
> if (!is_object($database))
> $database_object = new database(); // create new object
>
> return $database_object; // return address of object
> }
>
> Do I need to return $database_object; or return &$database_object;
>
> or
>
> When calling the function:
>
> $test = &database::getInstane()
> or
> $test = database::getInstane()
>
> 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

  #3  
Old July 17th, 2005, 11:49 AM
Mark
Guest
 
Posts: n/a

re: References in PHP5


"[Mystic]" <screwuspammer> 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_object; // reference object
>
> if (!is_object($database))
> $database_object = new database(); // create new object
>
> return $database_object; // 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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP5 and class inheritance question Logos answers 30 January 3rd, 2008 04:05 PM
__contruct in php4 not same behavior as php5 FFMG answers 8 November 22nd, 2007 06:05 AM
explicitly destroy Objects in PHP5 Thomas Ilsche answers 1 July 17th, 2005 01:42 PM
Problems getting "require" to work in PHP5 under Apache2 Mxsmanic answers 4 July 17th, 2005 11:07 AM