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(SqlPqrqm eter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparamet ers as &$param) {
if($param->equals($parame ter)) {
unset($param);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparamet ers = array_values($t his->commandparamet ers);
}
[/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(SqlPqrqm eter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparamet ers as $key=>&$value) {
if($value->equals($parame ter)) {
# delete the parameter form the internal array
unset($this->commandparamet ers[$key]);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparamet ers = array_values($t his->commandparamet ers);
}
[/PHP]
I don't know if this is THE solution to my problem, but it makes more sense to me now.
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(SqlPqrqm eter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparamet ers as &$param) {
if($param->equals($parame ter)) {
unset($param);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparamet ers = array_values($t his->commandparamet ers);
}
[/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(SqlPqrqm eter &$parameter) {
# check if the variable is in the array
# done by reference to prevent overhead copies
foreach($this->commandparamet ers as $key=>&$value) {
if($value->equals($parame ter)) {
# delete the parameter form the internal array
unset($this->commandparamet ers[$key]);
}
}
# break the reference
unset($param);
# restore the array
$this->commandparamet ers = array_values($t his->commandparamet ers);
}
[/PHP]
I don't know if this is THE solution to my problem, but it makes more sense to me now.