Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP mysqli wrapper: passing by reference with __call() and call_user_func_array()
    text
    copied!<p>I'm a long running fan of stackoverflow, first time poster. I'd love to see if someone can help me with this. Let me dig in with a little code, then I'll explain my problem. I have the following wrapper classes:</p> <pre><code>class mysqli_wrapper { private static $mysqli_obj; function __construct() // Recycles the mysqli object { if (!isset(self::$mysqli_obj)) { self::$mysqli_obj = new mysqli(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS, MYSQL_DBNAME); } } function __call($method, $args) { return call_user_func_array(array(self::$mysqli_obj, $method), $args); } function __get($para) { return self::$mysqli_obj-&gt;$para; } function prepare($query) // Overloaded, returns statement wrapper { return new mysqli_stmt_wrapper(self::$mysqli_obj, $query); } } class mysqli_stmt_wrapper { private $stmt_obj; function __construct($link, $query) { $this-&gt;stmt_obj = mysqli_prepare($link, $query); } function __call($method, $args) { return call_user_func_array(array($this-&gt;stmt_obj, $method), $args); } function __get($para) { return $this-&gt;stmt_obj-&gt;$para; } // Other methods will be added here } </code></pre> <p>My problem is that when I call <code>bind_result()</code> on the <code>mysqli_stmt_wrapper</code> class, my variables don't seem to be passed by reference and nothing gets returned. To illustrate, if I run this section of code, I only get NULL's:</p> <pre><code>$mysqli = new mysqli_wrapper; $stmt = $mysqli-&gt;prepare("SELECT cfg_key, cfg_value FROM config"); $stmt-&gt;execute(); $stmt-&gt;bind_result($cfg_key, $cfg_value); while ($stmt-&gt;fetch()) { var_dump($cfg_key); var_dump($cfg_value); } $stmt-&gt;close(); </code></pre> <p>I also get a nice error from PHP which tells me: <code>PHP Warning: Parameter 1 to mysqli_stmt::bind_result() expected to be a reference, value given in test.php on line 48</code></p> <hr> <p>I've tried to overload the <code>bind_param()</code> function, but I can't figure out how to get a variable number of arguments by reference. <code>func_get_args()</code> doesn't seem to be able to help either.</p> <p>If I pass the variables by reference as in <code>$stmt-&gt;bind_result(&amp;$cfg_key, &amp;$cfg_value)</code> it should work, but this is deprecated behaviour and throws more errors.</p> <p>Does anyone have some ideas around this? Thanks so much for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload