null;
$num = count($numbers);
$index =& $numbers[$num ? $num - 1 : $num];
$index = $i;
}
foreach ($numbers as $index) {
print "$indexn";
}
?>
Does not produce:
1
2
3
But instead:
1
2
2
Applying unset($index) before re-using the variable fixes this and the expected list will be produced:
1
2
3
It seems like PHP has problems with references, like that it can't work properly with circular references or free properly structure with more references. See http://bugs.php.net/?id=30053.
I have big problem with this and I hope someone from PHP add proper warning with explanation IN manual, if they can't fix it.
While trying to do object references with the special $this variable I found that this will not work:
class foo {
function bar() {
...
$this =& $some_other_foo_obj;
}
}
If you want to emulate this functionality you must iterate through the vars of the class and assign references like this:
$vars = get_class_vars('foo');
foreach (array_keys($vars) as $field) {
$this->$field =& $some_other_foo_obj->$field;
}
Now if you modify values within $this they will be modified within $some_other_foo_obj and vice versa.
Hope that helps some people!
p.s.
developer at sirspot dot com's note about object references doesn't seem correct to me.
$temp =& $object;
$object =& $temp->getNext();
Does the same exact thing as:
$object =& $object->getNext();
when you refernce $temp to $object all it does is make $temp an alias to the same memory as $object, so doing $temp->getNext(); and $object->getNext(); are calling the same function on the same object. Try it out if you don't believe me.
one very useful aspect for reference that i don't think i saw documented was the ability to skip a few steps with objects stored in objects.
for example:
assuming the object structure is correctly constructed (and mind you i haven't tried this in php, but it does work in most other high-end programming languages), instead of using this structure to get a variable/function
//start
$obj1 -> obj2 -> obj3 -> varX = 0;
$obj1 -> obj2 -> obj3 -> varY = 0;
$obj1 -> obj2 -> obj3 -> functionX();
$obj1 -> obj2 -> obj3 -> functionY();
//end
you can use this method:
//start
$tempObj = & $obj1 -> obj2 -> obj3;
$tempObj -> varX = 0;
$tempObj -> varY = 0;
$tempObj -> functionX();
$tempObj -> functionY();
//end
23/24 首页 上一页 21 22 23 24 下一页 尾页 |