Fog Creek Software
Discussion Board




PHP and references

I know that I can force PHP to pass-by-reference to a function by prefixing an argument in the function declaration with '&'.

However, say I want to assign a reference to an object to a variable.  What if I have $Object1 and I want $reference_to_object_1.  Can I:

$reference_to_object_1 = &$Object1 ?

Yeah, I could test this out but I havn't got access to my hosting from where I am, and no means to install PHP.  So I'm posting.  Deal with it.  =]

muppet
Friday, July 16, 2004

Hmm, that's certainly a nice friendly way to ask for help. 

Herbert Sitz
Friday, July 16, 2004

I thought it was.  Thanks for noticing.

You sound like you *could* use a sitz.

muppet
Friday, July 16, 2004

Yes.

http://www.php.net/manual/en/language.variables.php

Jiminy Cricket
Friday, July 16, 2004

oh hey look at that, it's right in the manual.

D'oh.

The 400-some-odd page book I have from Wrox on PHP never once mentions that syntax.  Not once.

sheez.

muppet
Friday, July 16, 2004

Stop leeching of us! What next? Take our jobs?

Stalking the trolls
Friday, July 16, 2004

Damn straight.  I'll take all of your jobs.  And I'll do them better than you.  In less time.  Hell, I hardly spend more than 30 minutes a day working at my CURRENT job.  I'm a much better programmer than all of you.  Clearly I'm a gifted genius.

muppet
Friday, July 16, 2004

Go back to India you no good scab!!!

How was that?

anon-y-mous cow-ard
Friday, July 16, 2004

It lacked profanity.  You have to curse like a redneck if you really want people to take you seriously when you're spouting racial slurs.

muppet
Friday, July 16, 2004

Don't take my jerb.

Bored Bystander
Friday, July 16, 2004

The typical way to write that is:

$object1 =& $object2

Treating the =& as some kind of assign by reference operator.

Almost Anonymous
Friday, July 16, 2004

If you upgrade to version 5, by the way, pass by reference is the default for objects.  Typing gets a little stronger too.  Not like C or C++ typing, but an object remembers what its members are supposed to be.

Clay Dowling
Friday, July 16, 2004

get your own herb.

pfffst
Friday, July 16, 2004

haha Bored Bystander represents with the South Park line

fark that was a funny episode

Dan G
Friday, July 16, 2004

Nothing beats The (PHP) Manual!  All questions are answered therein.  All wisdom is contained therein.  It is the gateway to all things PHP.

Did you know you can type http://www.php.net/your_search_term_here in a browser's address field and php.net will automatically search the manual for the term and take you to the results?  It only searches the functions section of the manual, so in the case of the question about references, it wouldn't have worked, but it's fun to know anyway.

OffMyMeds
Friday, July 16, 2004

I'll tell you what's really fucking annoying about PHP and references.

Say you have an array of objects.

$foo = array( new Bar(1), new Bar(2) );

Now say you want to change these objects /in place/, or pass a reference to each of these objects to some function one by one. You'd expect to be able to do something like

foreach ($foo as $f) {
    $f->changesomething();
}

But oh no. foreach actually makes a COPY of each element of the array $foo as it goes along, and puts the COPY in $f. There's no pleasant syntax that will get it to just make a reference, so you end up doing something ugly like

foreach (array_keys($foo) as $k) {
  $foo[$k]->dosomething();
  dosomethingelse(&$foo[$k]);
}

Yes what appears to be just a list is actually stored as an associative array with numeric keys in PHP, for some misguided reason.

Matt
Saturday, July 17, 2004

Matt, try:
foreach ($foo as $K=>$f) {
    $foo[$k]->changesomething();
}

Jordan Lev
Saturday, July 17, 2004

Yeah, I know, that still involves the overhead of copying the object into $f each time though, for no real reason.

I believe PHP5 makes references by default, rather than copies, so hopefully it'll be fixed there.

Matt
Saturday, July 17, 2004

"Yeah, I know, that still involves the overhead of copying the object into $f each time though, for no real reason."

PHP uses copy-on-write so there is no overhead in copying the object into $f if you don't use it.  I also believe there is a syntax that will allow you to use references in foreach

"I believe PHP5 makes references by default, rather than copies, so hopefully it'll be fixed there."

Yes, it is.

Almost Anonymous
Saturday, July 17, 2004

I wonder how backwards compatible PHP5 is

::sigh::

my CMS is just beginning to come together in PHP4  :P

http://test.madebymonkeys.net

muppet
Saturday, July 17, 2004

The only way in which it won't be backwards compatible is if you used certain rather obscure keywords in strange an unnatural ways, at least according to the upgrade notes.

Alas, I don't has PHP5 even on a development server, so I can't play with it the way that I want to really test compatability and abuse new features.  Only on a single workstation, and that's not my workstation.

Clay Dowling
Sunday, July 18, 2004

>>>I also believe there is a syntax that will allow you to use references in foreach

Really? I've looked quite hard for that and not found any, but would be interested if there was.

Copy on write - so you mean it is making references behind the scenes and not actually doing a physical copy unless/until the copy is actually written to? neat. Doesn't make the syntax any less irritating though.

Matt
Sunday, July 18, 2004

Yeah, I've also written a quite complex CMS / relational database editing app in PHP4... it's really an appalling language for anything of that level of complexity :-( I wish I'd had PHP5 at the time (the client insisted on PHP), although to be honest you may aswell just use Java or something like Python as PHP5 is just PHP4 with java/python -esque contructs bolted onto it, leaving most of the original annoyances unchanged for backwards compatibility reasons.

Matt
Sunday, July 18, 2004

*  Recent Topics

*  Fog Creek Home