Connecting Tech Pros Worldwide Forums | Help | Site Map

References in PHP5

[Mystic]
Guest
 
Posts: n/a
#1: Jul 17 '05
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



Rutger Claes
Guest
 
Posts: n/a
#2: Jul 17 '05

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

Mark
Guest
 
Posts: n/a
#3: Jul 17 '05

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