Can someone please tell me if this method of doing foreach has drawbacks?
class someclass {
function foo() {
foreach ($my_array as $this->key => $this->value) {
$this->bar();
$this->baz();
}
}
function bar(){
//do something with $this->key or $this->value
}
function baz(){
//do something with $this->key or $this->value
}
}
It’s pretty inefficient as you are essentially setting the key to an associative array on each loop. I would keep them local and then assign them when the loop is done if you need to store them. Also, pass the values to the methods when you call them.