473,379 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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
Jul 17 '05 #1
2 1442
"[Mystic]" <screwuspammer> wrote:
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


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

Rutger
--
Rutger Claes rg*@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

Jul 17 '05 #2
"[Mystic]" <screwuspammer> wrote:
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
}


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.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: neur0maniak | last post by:
Hi, I've been eager to try out PHP5, so I've dumped it on my little dev machine. It's running WinXP with IIS5. I've put the php-cgi.exe in the "mappings" page as I'm used to doing with PHP4....
4
by: badbetty | last post by:
Dear Googlers I have installed PHP5 to run on WinXP against Apache 2. It works! ie. I have tested a few simple scripts and a basic xml document parse. I now want to try the XSL extension so I...
5
by: Aziz | last post by:
Hi, I've recently contacted technical service of a web hosting company and asked them wheter or not they're gonna upgrade to PHP5 and MySQL5. Here's a quote from their response which confused me...
4
by: Chuck Anderson | last post by:
I am trying to install Php5 on my WindowsXP machine (at home, not on the 'net). I downloaded the Php Windows binary at php.net, unzipped it to c:/Php5, changed my Apache config file to use Php5...
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
3
by: howa | last post by:
according to the php manual, it said: Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own. I doubt if this only apply to the PHP5...
19
by: McKirahan | last post by:
I am working in two environments neither configuration of which I can change; one's my Web host the other a client. My Web host requires the use of the ".php5" extension to use PHP v5.1.4; where...
2
by: amygdala | last post by:
I'm trying to figure out the correct use of references. Lets say I have a class Validator and a class User. And I want to let a User instance be an referenced object in Validator. Where would the...
3
by: xhe | last post by:
I have just upgraded my php version form php4 to php5. and I met this problem, and don't know if you know the solution. My site was written in PHP4, and most parts can be running smoothly in PHP5,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.