PHP mysqli wrapper: passing by reference with __call() and call_user_func_array()

随声附和 提交于 2019-12-01 11:31:46

If you'll extend from the mysqli_stmt class you'll bypass the reference problem. (which has no clean solution)

class mysqli_stmt_wrapper extends mysqli_stmt {
  public function __construct($link, $query) {
    parent::__construct($link, $query);
  }
}

class mysqli_wrapper extends mysqli {
  public function prepare($query) {
    return new mysqli_stmt_wrapper($this, $query);
  }
}

With a little help from the guys from the #php irc channel I came up with the following solution:

// We have to explicitly declare all parameters as references, otherwise it does not seem possible to pass them on without
// losing the reference property.
public function bind_result (&$v1 = null, &$v2 = null, &$v3 = null, &$v4 = null, &$v5 = null, &$v6 = null, &$v7 = null, &$v8 = null, &$v9 = null, &$v10 = null, &$v11 = null, &$v12 = null, &$v13 = null, &$v14 = null, &$v15 = null, &$v16 = null, &$v17 = null, &$v18 = null, &$v19 = null, &$v20 = null, &$v21 = null, &$v22 = null, &$v23 = null, &$v24 = null, &$v25 = null, &$v26 = null, &$v27 = null, &$v28 = null, &$v29 = null, &$v30 = null, &$v31 = null, &$v32 = null, &$v33 = null, &$v34 = null, &$v35 = null) {
    // debug_backtrace returns arguments by reference, see comments at http://php.net/manual/de/function.func-get-args.php
    $trace = debug_backtrace();
    $args = &$trace[0]['args'];
    return call_user_func_array(array($this->mysqlObj, 'bind_result'), $args);
}

I'm guessing this is because the original function signature specifies that it expects references, whereas your __call cannot do so. Therefore, try not using __call but explicitly adding the bind_result with the same function signature as the original.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!