The following results in error PHP Fatal error: Constant expression contains invalid operations
. I can obviously create a new method updateArrayOfNodes()
, however, I don't care to do so for the marginal performance increase. I think the performance to repeatably re-create the prepared statement is a bigger deal.
How can I reuse the prepared statement within a method?
class mapper
{
public function update(Entities\Node $node){
static $stmt=$this->pdo->prepare('UPDATE series SET name=:name, position=:position WHERE id=:id');
return $stmt->execute(['name'=>$node->name, 'position'=>$node->position, 'id'=>$node->id]);
}
}
$mapper=new mapper();
foreach($arr as $node) $mapper->update($node);