无法使用字符串键解压缩数组
2022-08-30 10:10:23
错误消息是:
致命错误未捕获错误:无法解压缩带有字符串键的数组
我知道我可以简单地运行该方法两次并传递 and ,但我试图掌握使用新的解包值。我想像这样传递值:fetch()
['q']
['bind']
...
(string) SQL, (Array) Bind Values
但我认为它试图从方法中解压缩绑定值数组以及响应数组。是否可以解压缩此数组?fetch()
它看起来像这样:
array(2) {
["q"] => string(7) "example"
["bind"] => array(1) {
["example"] => string(3) "one"
}
}
这是整个代码,以防您需要查看它们是如何组合在一起的:
class ModelController {
public static function execute($sql, $vals) {
var_dump($vals);
}
}
class ModelContainer {
private $queries = [];
public function add_model(Model $model, $name) {
$this->queries[$name] = $model;
return $this;
}
public function exec_all() {
foreach($this->queries as $q) {
ModelController::execute(...$q->fetch());
}
}
public function exec($name) {
}
}
class Model {
private $sql;
private $vals = [];
public function set_q($statement) {
$this->sql = $statement;
return $this;
}
public function bind($vals = []) {
$this->vals = $vals;
return $this;
}
public function fetch() {
return ['q' => (string)$this->sql,
'bind' => $this->vals ];
}
}
$m = new ModelContainer();
$m->add_model((new Model)->set_q('example SQL content here')->bind(['example' => 'example value here']), 'one');
$m->exec_all();