diff --git a/src/Mysqli/MysqliStatement.php b/src/Mysqli/MysqliStatement.php index 3e8118bb..a2f46b5c 100644 --- a/src/Mysqli/MysqliStatement.php +++ b/src/Mysqli/MysqliStatement.php @@ -31,7 +31,7 @@ class MysqliStatement implements StatementInterface * @var array * @since 2.0.0 */ - protected $bindedValues; + protected $bindedValues = []; /** * Mapping between named parameters and position in query. @@ -39,7 +39,7 @@ class MysqliStatement implements StatementInterface * @var array * @since 2.0.0 */ - protected $parameterKeyMapping; + protected $parameterKeyMapping = []; /** * Mapping array for parameter types. @@ -301,27 +301,23 @@ public function bindParam($parameter, &$variable, string $dataType = ParameterTy */ private function bindValues(array $values) { - $params = []; $types = str_repeat('s', \count($values)); - if (!empty($this->parameterKeyMapping)) { - foreach ($values as $key => &$value) { + if ($this->parameterKeyMapping !== []) { + $params = []; + foreach ($values as $key => $value) { $paramKey = $this->parameterKeyMapping[$key]; foreach ($paramKey as $currentKey) { - $params[$currentKey] =& $value; + $params[$currentKey] = $value; } } ksort($params); - } else { - foreach ($values as $key => &$value) { - $params[] =& $value; - } - } - array_unshift($params, $types); + return $this->statement->bind_param($types, ...$params); + } - return \call_user_func_array([$this->statement, 'bind_param'], $params); + return $this->statement->bind_param($types, ...$values); } /** @@ -372,22 +368,22 @@ public function errorInfo() */ public function execute(?array $parameters = null) { - if ($this->bindedValues !== null) { + if ($this->bindedValues !== []) { $params = []; $types = []; - if (!empty($this->parameterKeyMapping)) { - foreach ($this->bindedValues as $key => &$value) { + if ($this->parameterKeyMapping !== []) { + foreach ($this->bindedValues as $key => $value) { $paramKey = $this->parameterKeyMapping[$key]; foreach ($paramKey as $currentKey) { - $params[$currentKey] =& $value; + $params[$currentKey] = $value; $types[$currentKey] = $this->typesKeyMapping[$key]; } } } else { - foreach ($this->bindedValues as $key => &$value) { - $params[] =& $value; + foreach ($this->bindedValues as $key => $value) { + $params[] = $value; $types[$key] = $this->typesKeyMapping[$key]; } } @@ -395,10 +391,8 @@ public function execute(?array $parameters = null) ksort($params); ksort($types); - array_unshift($params, implode('', $types)); - try { - \call_user_func_array([$this->statement, 'bind_param'], $params); + $this->statement->bind_param(implode('', $types), ...$params); } catch (\Exception $e) { throw new PrepareStatementFailureException($e->getMessage(), $e->getCode(), $e); } @@ -420,15 +414,7 @@ public function execute(?array $parameters = null) $meta = $this->statement->result_metadata(); if ($meta !== false) { - $columnNames = []; - - foreach ($meta->fetch_fields() as $col) { - $columnNames[] = $col->name; - } - - $meta->free(); - - $this->columnNames = $columnNames; + $this->columnNames = array_column($meta->fetch_fields(), 'name'); } else { $this->columnNames = false; } @@ -438,13 +424,10 @@ public function execute(?array $parameters = null) $this->statement->store_result(); $this->rowBindedValues = array_fill(0, \count($this->columnNames), null); - $refs = []; - - foreach ($this->rowBindedValues as $key => &$value) { - $refs[$key] =& $value; - } + // The following is necessary as PHP cannot handle references to properties properly + $refs =& $this->rowBindedValues; - \call_user_func_array([$this->statement, 'bind_result'], $refs); + $this->statement->bind_result(...$refs); } $this->result = true;