Hello,
I've written a small function to delete a variable from a class' internal array.
The variables on their own are custom class objects, and I'm a bit confused about the whole pass by reference thing.
First I'll show you the code:
[PHP]
# removes a parameter from the array.
# variables are passed as reference to prevent overhead.
public function delVar(SqlPqrqmeter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparameters as &$param) {
if($param->equals($parameter)) {
unset($param);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparameters = array_values($this->commandparameters);
}
[/PHP]
I'm confused about the unset part.I know I need to call unset on my $param value after the foreach loop to break the reference with the internal array, But I wish to destroy the variable thats holding the position inside the array is it matches the value of the provided parameter.
Am I using the unset function correctly there or should I do it differently ?
EDIT
-------
Changed the code into this :
[PHP]
# removes a parameter from the array.
# variables are passed as reference to prevent overhead.
public function delVar(SqlPqrqmeter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparameters as $key=>&$value) {
if($value->equals($parameter)) {
# delete the parameter form the internal array
unset($this->commandparameters[$key]);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparameters = array_values($this->commandparameters);
}
[/PHP]
I don't know if this is THE solution to my problem, but it makes more sense to me now.