By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

References

P: n/a
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
If I assign the reference as below;
$var = getRow()
and then echo the result;
echo $var['name'];
echo $var['address'];
it all works fine, which surprises me somewhat.
In other languages I have more experience with this would throw an error, or
leave you with a reference to nothing, however in php 5 I can use this
variable hereon without a problem.
Does php make a copy of the result if there is no var to actualy pass a
reference to?
I can't see how it isn't.
An explaination of this would be very appreciated.
TIA,
Vince
May 6 '07 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
But you do have something to reference. odbc_fetch_array returns an
array, and you are returning a reference to that array.
If I assign the reference as below;
$var = getRow()
and then echo the result;
echo $var['name'];
echo $var['address'];
it all works fine, which surprises me somewhat.
No surprise at all. It should work that way.
In other languages I have more experience with this would throw an error, or
leave you with a reference to nothing, however in php 5 I can use this
variable hereon without a problem.
It won't in C++ or Java if odbc_fetch_array returns a reference. It
will work just fine.
Does php make a copy of the result if there is no var to actualy pass a
reference to?
All a variable is is a place to keep something to use later. You don't
need to use it again in this function, so there's no need to create a
variable. Just return the array directly, as is being done.
I can't see how it isn't.
An explaination of this would be very appreciated.
TIA,
Vince


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '07 #2

P: n/a
On May 6, 9:24 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
All a variable is is a place to keep something to use later.
It would make life simpler if that were so, yes.

May 7 '07 #3

P: n/a
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:xM******************************@comcast.com. ..
Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.

But you do have something to reference. odbc_fetch_array returns an
array, and you are returning a reference to that array.
If it passing an array to $var then it isn't behaving as a reference, at
least not as I understand it. It would be behaving as though I had
requested passing the array itself in the call wouldn't it?
No surprise at all. It should work that way.
Why is that?
It won't in C++ or Java if odbc_fetch_array returns a reference.
I would have thought that the '&' modifier would do the same in php too
Jerry.
All a variable is is a place to keep something to use later. You don't
need to use it again in this function, so there's no need to create a
variable. Just return the array directly, as is being done.
Yes, it is assigning an array evidently, rather than a reference to an array
so that would mean that the request for a reference it being ignored here?

Regards,
Vince
May 7 '07 #4

P: n/a
Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
Are you debugging with E_ALL? This actually should throw a notice ("only
variables can be returned by ref" or similar).
If I assign the reference as below;
$var = getRow()
and then echo the result;
echo $var['name'];
echo $var['address'];
it all works fine, which surprises me somewhat.
In other languages I have more experience with this would throw an error, or
leave you with a reference to nothing, however in php 5 I can use this
variable hereon without a problem.
Does php make a copy of the result if there is no var to actualy pass a
reference to?
I can't see how it isn't.
An explaination of this would be very appreciated.
TIA,
Vince



--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 7 '07 #5

P: n/a
"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.

Are you debugging with E_ALL? This actually should throw a notice ("only
variables can be returned by ref" or similar).
Yes Gosha. No warning or notice however.

Vince
May 7 '07 #6

P: n/a
Vince Morgan wrote:
"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
>Vince Morgan wrote:
>>I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
Are you debugging with E_ALL? This actually should throw a notice ("only
variables can be returned by ref" or similar).

Yes Gosha. No warning or notice however.

Vince

Report a bug then. For reference, the followin throws a notice in php 5.2:

function &foo($rc) {
return mysql_fetch_array($rc);
}
$rc = mysql_query("SELECT 1");
$a = foo($rc);
// NOTICE: Only variable references should be returned by reference
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 7 '07 #7

P: n/a

"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
Vince Morgan wrote:
Are you debugging with E_ALL? This actually should throw a notice
("only
variables can be returned by ref" or similar).
Yes Gosha. No warning or notice however.

Vince

Report a bug then. For reference, the followin throws a notice in php 5.2:

function &foo($rc) {
return mysql_fetch_array($rc);
}
$rc = mysql_query("SELECT 1");
$a = foo($rc);
// NOTICE: Only variable references should be returned by reference
I will report it as a bug Gosha.
The following doesn't report a warning or notice either, which is at least
consistent;
function &ret()
{
return 1+1;
}
$d=ret();
echo ret;
Output is 2.

Thanks,
Vince


May 7 '07 #8

P: n/a
Razzbar wrote:
On May 6, 9:24 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>All a variable is is a place to keep something to use later.

It would make life simpler if that were so, yes.


No, that's all it is. Nothing more, nothing less.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 7 '07 #9

P: n/a
Vince Morgan wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:xM******************************@comcast.com. ..
>Vince Morgan wrote:
>>I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
But you do have something to reference. odbc_fetch_array returns an
array, and you are returning a reference to that array.

If it passing an array to $var then it isn't behaving as a reference, at
least not as I understand it. It would be behaving as though I had
requested passing the array itself in the call wouldn't it?
You do not "pass" anything to a variable. You pass things to functions,
but you *assign* things to an array.

All you are doing in your getRow() is returning a reference to the array
returned by ordb_fetch_array. It is really no different than:

function &getRow() {
$x = odbc_fetch_array($this->_det);
return $x;
}

You just left out the middle step, which is perfectly legitimate.

And once you get back, it is assigning that reference to the array to
$var.
>No surprise at all. It should work that way.

Why is that?
>It won't in C++ or Java if odbc_fetch_array returns a reference.

I would have thought that the '&' modifier would do the same in php too
Jerry.
You really should watch what you quote. The entire quote was:
>In other languages I have more experience with this would throw an
error, or leave you with a reference to nothing, however in php 5 I
can use this variable hereon without a problem.
>It won't in C++ or Java if odbc_fetch_array returns a reference. It
will work just fine.
And it won't cause an error in C++ or Java because it's perfectly valid
code. In either case you're returning a reference.
>All a variable is is a place to keep something to use later. You don't
need to use it again in this function, so there's no need to create a
variable. Just return the array directly, as is being done.

Yes, it is assigning an array evidently, rather than a reference to an array
so that would mean that the request for a reference it being ignored here?

Regards,
Vince

What do you expect to be different? Operations on a reference are
exactly the same as operations on a copy. The *only* difference in your
code is that if you change a reference, you change the original. if you
change a copy, you do not.

And if you're just accessing (and not changing) the any values, you have
no way of telling if you're acting on a copy or a reference, unless you
go back to see how it was passed or returned.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 7 '07 #10

P: n/a
gosha bine wrote:
Vince Morgan wrote:
>"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
>>Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing to
actualy reference.
Are you debugging with E_ALL? This actually should throw a notice ("only
variables can be returned by ref" or similar).

Yes Gosha. No warning or notice however.

Vince


Report a bug then. For reference, the followin throws a notice in php 5.2:

function &foo($rc) {
return mysql_fetch_array($rc);
}
$rc = mysql_query("SELECT 1");
$a = foo($rc);
// NOTICE: Only variable references should be returned by reference

Gosha,

But you're dealing with a different function, so the results may vary.

Take the following script in 5.2:

<?php

function &getArrayRef() {
static $ar = array();
return $ar;
}

function getArrayCopy() {
static $ar = array();
return $ar;
}

function &getGetArrayRef() {
return getArrayRef();
}

function &getGetArrayCopy() {
return getArrayCopy();
}

echo "Return by ref:\n";
$var1 = getGetArrayRef();
echo "Return by copy:\n";
$var2 = getGetArrayCopy();

?>

Output:

Return by ref:
Return by copy:

Notice: Only variable references should be returned by reference in
test.php on line 18

Notice that when getArrayRef returns a reference, getGetArrayRef can
return that reference with no notice. However, since getArrayCopy
returns a copy, getGetArrayCopy gets a NOTICE when it tries to return
the reference.

It looks like odbc_fetch_array is acting like the former, and
mysql_fetch_array is acting like the latter. The former way is
obviously more efficient (and flexible). But changing an array member
may have undesired effects. It's hard to know when you don't know what
the code that created the array is doing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 7 '07 #11

P: n/a
Vince Morgan wrote:
"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
>Vince Morgan wrote:
>>>Are you debugging with E_ALL? This actually should throw a notice
("only
>>>variables can be returned by ref" or similar).
Yes Gosha. No warning or notice however.

Vince

Report a bug then. For reference, the followin throws a notice in php 5.2:

function &foo($rc) {
return mysql_fetch_array($rc);
}
$rc = mysql_query("SELECT 1");
$a = foo($rc);
// NOTICE: Only variable references should be returned by reference

I will report it as a bug Gosha.
The following doesn't report a warning or notice either, which is at least
consistent;
It does on my system. Before filing a bug, make sure 1) you're using php
5.2.0 or later and 2) you have proper error_reporting and display_errors
settings.

function &ret()
{
return 1+1;
}
$d=ret();
echo ret;
Output is 2.

Thanks,
Vince


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 7 '07 #12

P: n/a
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:Ht******************************@comcast.com. ..
Vince Morgan wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:xM******************************@comcast.com. ..
Vince Morgan wrote:
I guess I don't understand how references work in php;
Calling a class member function below as an example;
function &getRow()
{
return odbc_fetch_array($this->_det);
}
I would expect that I should end up with nothing as there is nothing
to
>actualy reference.
But you do have something to reference. odbc_fetch_array returns an
array, and you are returning a reference to that array.
If it passing an array to $var then it isn't behaving as a reference, at
least not as I understand it. It would be behaving as though I had
requested passing the array itself in the call wouldn't it?

You do not "pass" anything to a variable. You pass things to functions,
but you *assign* things to an array.
Yes, I'm sorry it's getting late here Jerry.
All you are doing in your getRow() is returning a reference to the array
returned by ordb_fetch_array. It is really no different than:

function &getRow() {
$x = odbc_fetch_array($this->_det);
return $x;
}

You just left out the middle step, which is perfectly legitimate.

And once you get back, it is assigning that reference to the array to
$var.
Actualy, it seems to be assigning a copy to $var rather than a reference to
it.
A simple test seems to verify this.

<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=$r;
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5 as not expected, by me that is ;)
$var=$r;
echo $var;//output is now 6
?>

If it was actualy a reference to $r that was being assigned, the second echo
should also be 6.
On my machine with 5.0.33 the output is as described above.
You really should watch what you quote. The entire quote was:
Yes, you are right Jerry, My appologies. I plead the late thing again.
>In other languages I have more experience with this would throw an
>error, or leave you with a reference to nothing, however in php 5 I
>can use this variable hereon without a problem.
>It won't in C++ or Java if odbc_fetch_array returns a reference. It
>will work just fine.
Yes, you are correct on both accounts. I didn't read the "if
odbc_fetch_array returns a reference" bit correctly.

I wouldn't expect odbc_etc() to return a reference, without at least
declaring this in the docs, but then again I'm still learning.

Regards,
Vince
May 7 '07 #13

P: n/a
"Vince Morgan" <vinharAtHereoptusnet.com.auwrote in message
news:46***********************@news.optusnet.com.a u...
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=$r;
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5 as not expected, by me that is ;)
$var=$r;
echo $var;//output is now 6
?>
Oooops, getting too late evidently. I meant to write the following.
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=get();
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5
$var=get();
echo $var;//output is now 6
?>
May 7 '07 #14

P: n/a
"gosha bine" <st********@gmail.comwrote in message
news:46**********************@read.cnntp.org...
Vince Morgan wrote:
"gosha bine" <st********@gmail.comwrote in message
I will report it as a bug Gosha.
The following doesn't report a warning or notice either, which is at
least
consistent;

It does on my system. Before filing a bug, make sure 1) you're using php
5.2.0 or later and 2) you have proper error_reporting and display_errors
settings.
Error_reporting is "error_reporting = E_ALL", and "display_errors = On"
I need to upgrade. I'm using 5.0.33.
Thank you Gosha,
Vince
May 7 '07 #15

P: n/a
Vince,

Almost - but the assignment into $var is an operation, also. More below.

Vince Morgan wrote:
"Vince Morgan" <vinharAtHereoptusnet.com.auwrote in message
news:46***********************@news.optusnet.com.a u...
><?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=$r;
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5 as not expected, by me that is ;)
$var=$r;
echo $var;//output is now 6
?>

Oooops, getting too late evidently. I meant to write the following.
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=get();
get() is returning a reference. But you are assigning a copy into $var.
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5
$var=get();
Same as above.
echo $var;//output is now 6
?>

If you change these to

$var = $get();

You will get 5, 6 and 6, respectively.

There's a discussion about it at
<http://www.php.net/manual/en/language.references.return.php>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 7 '07 #16

P: n/a

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:kp******************************@comcast.com. ..
Vince,

Almost - but the assignment into $var is an operation, also. More below.

Vince Morgan wrote:
"Vince Morgan" <vinharAtHereoptusnet.com.auwrote in message
news:46***********************@news.optusnet.com.a u...
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=$r;
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5 as not expected, by me that is ;)
$var=$r;
echo $var;//output is now 6
?>
Oooops, getting too late evidently. I meant to write the following.
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=get();

get() is returning a reference. But you are assigning a copy into $var.
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5
$var=get();

Same as above.
echo $var;//output is now 6
?>

If you change these to

$var = $get();

You will get 5, 6 and 6, respectively.

There's a discussion about it at
<http://www.php.net/manual/en/language.references.return.php>
Thank you for taking the time Jerry.
I haven't the time just yet to read the entire linked article, but have read
enough already to realize my understanding of references in php is
incorrect.
Thanks again,
Vince
May 8 '07 #17

P: n/a
Vince Morgan wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:kp******************************@comcast.com. ..
>Vince,

Almost - but the assignment into $var is an operation, also. More below.

Vince Morgan wrote:
>>"Vince Morgan" <vinharAtHereoptusnet.com.auwrote in message
news:46***********************@news.optusnet.com .au...
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=$r;
echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5 as not expected, by me that is ;)
$var=$r;
echo $var;//output is now 6
?>
Oooops, getting too late evidently. I meant to write the following.
<?
function &get()
{
global $r;
return $r;
}
$r=5;
$var=get();
get() is returning a reference. But you are assigning a copy into $var.
>>echo $var;//output is 5 as expected.
$r=6;
echo $var;//output is still 5
$var=get();
Same as above.
>>echo $var;//output is now 6
?>

If you change these to

$var = $get();

You will get 5, 6 and 6, respectively.

There's a discussion about it at
<http://www.php.net/manual/en/language.references.return.php>
Thank you for taking the time Jerry.
I haven't the time just yet to read the entire linked article, but have read
enough already to realize my understanding of references in php is
incorrect.
Thanks again,
Vince

Vince,

I wouldn't say it's incorrect. Maybe just a little incomplete. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 8 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.