I'm totally aware of that. But the difference between my foreach-loop and your for-loop is
that in your loop the same value is echoed everytime, whereas my loop actually traverses
the $_POST-array. But then, you set yourself straight. :-)
By the way, more often than not you can't use foreach, even though it seems nice. When
doing this:
PHP Code:
foreach ($array as $key => $value) { ... }
$key and $value become copies of the current array element that changes at each
iteration of the loop. The problem arises when you store objects (or references) inside an
array. With foreach you get a copy of every object, and not a reference to it, so
calling a method on an object that changes its state has no effect. E.g.:
PHP Code:
foreach ($objects as $object)
{
$object->setSomeVariable('someValue');
}
This doesn't work! It's extremely sad, but that doesn't make it less true.
I think the problem stems from the difference between 'passing by value' and 'passing by
reference'. Compared to Java, PHP does things a bit different. (In Java, there is no
difference; parameters are always passed by value.)
'Passing by value' in Java means that when you assign a value to an argument inside a
function, the value won't change on the outside. This is the same as in PHP. For example:
PHP Code:
function test($i)
{
$i = 12;
}
$i = 8;
test($i);
echo $i; // prints '8'
Now comes the difference between Java and PHP. In Java, objects are always passed
by value, but you can call methods on an object inside a function that changes its value.
For example (Java):
PHP Code:
void setValue(Object o, Value v)
{
o.setValue(v);
}
To be explicit: the object 'o' is passed by value, but it is still possible to call a
method on it that changes its inner state.
In PHP this doesn't work: you'd be changing the value of a copy of the object, and
not of the original object. To solve this problem, you must pass the object by
reference, as in this example:
PHP Code:
function setValue(&$object, $value)
{
$object->setValue($value);
}
However, there is a problem. It is now possible to do this:
PHP Code:
function setValue(&$object, $value)
{
$object = new Object($value);
}
Which isn't very nice (because all other properties of the passed object are lost). The
only way to overcome this problem is by following programming rules like this one:
'Never assign to references passed to functions.'. Generally, it is considered bad
practice to assign to any parameter passed to a function, be it passed by value or
by reference, so this rule shouldn't too hard to follow, but still it's something you
have to enforce on yourself.
I won't say that PHP's approach to passing by value is bad, or that Java's approach is
better. PHP looks a lot more like C(++) than Java, and I personally prefer C++ above
Java (but I hate C). It's just a design decision, and one you should be aware of!
To conclude: when you use objects in PHP, you should always pass (and return!) them
by reference. When storing objects (or references) inside an array, don't use foreach to
iterate over the array elements, but use some other method instead. (See the Annotated PHP
Manual for more information on how to do this.)
Vincent
Bookmarks