Connecting Tech Pros Worldwide Help | Site Map

References in PHP5

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:48 AM
[Mystic]
Guest
 
Posts: n/a
Default 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_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, 10:48 AM
Rutger Claes
Guest
 
Posts: n/a
Default 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, 10:49 AM
Mark
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.