You are here: Re: References « PHP Programming Language « IT news, forums, messages
Re: References

Posted by Vince Morgan on 05/07/07 14:05

"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:Ht2dnUWaW4HkvqLbnZ2dnUVZ_gydnZ2d@comcast.com...
> Vince Morgan wrote:
> > "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> > news:xMGdnWvEq_C8eKDbnZ2dnUVZ_uqvnZ2d@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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация