473,408 Members | 1,858 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,408 software developers and data experts.

References

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
17 1226
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
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
"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
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
"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
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

"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
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
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
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
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
"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
"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
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.