Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

PHP unfortunately has the same issue because it copied Java's terminology to describe object reference values (introduced in PHP 5), yet it has always also had "references" which are actually pass-by-reference.

To demonstrate, object references/pointers/whatever:

  $foo = new Foo(); // $foo
  $foo2 = $foo;
  $foo2->x = 3;
  // $foo->x is also 3 now, $foo and $foo2 point to the same Foo object
  // that is, $foo and $foo2 both contain an object handle/pointer/reference/whatever that points to the same Foo object
PHP references:

  function swap(&$a, &$b) {
      $temp = $a;
      $a = $b;
      $b = $temp;
  }
  $a = 3;
  $b = 2;
  swap($a, $b);
  // $a is now 2, $b is now 3


The only reason I use @-refs anymore is because of PHP copying arrays on assignment (shallowly, lazily.) In addition--unless you do everything in a super-functional way--you need to unset() each &-ref to prevent latent bugs later in the same scope.

It's an unfortunate situation, and it gets worse when you add array-like-objects into the mix, which look exactly like "normal" arrays up until the moment everything explodes into WTFs.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: