views:

315

answers:

3

Hi everyone. 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:

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->$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->stmt_obj = mysqli_prepare($link, $query);
    }
    function __call($method, $args)
    {
        return call_user_func_array(array($this->stmt_obj, $method), $args);
    }
    function __get($para)
    {
        return $this->stmt_obj->$para;
    }
    // Other methods will be added here
}

My problem is that when I call bind_result() on the mysqli_stmt_wrapper 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:

$mysqli = new mysqli_wrapper;

$stmt = $mysqli->prepare("SELECT cfg_key, cfg_value FROM config");
$stmt->execute();
$stmt->bind_result($cfg_key, $cfg_value);

while ($stmt->fetch())
{
    var_dump($cfg_key);
    var_dump($cfg_value);
}
$stmt->close();

I also get a nice error from PHP which tells me: PHP Warning: Parameter 1 to mysqli_stmt::bind_result() expected to be a reference, value given in test.php on line 48


I've tried to overload the bind_param() function, but I can't figure out how to get a variable number of arguments by reference. func_get_args() doesn't seem to be able to help either.

If I pass the variables by reference as in $stmt->bind_result(&$cfg_key, &$cfg_value) it should work, but this is deprecated behaviour and throws more errors.

Does anyone have some ideas around this? Thanks so much for your time.

A: 

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.

janmoesen
Yea, that's why I say, I've tried to overload `bind_result`, but the problem is that the `bind_result` function can accept any number of arguments, and it need each to be passed by reference. `func_get_args()` doesn't seem to do this for me.
Dave
Oh, right, didn't quite catch that. Sorry! I think you're in a bit of a bind, here. Badum! (And sorry again, for that.)
janmoesen
+1  A: 

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);
  }
}
Bob Fanger
Hi bob. Thanks, that's perfect! I didn't realise you could just construct a new `mysqli_stmt` object like that. I thought some other 'magic stuff' was happening inside the original `mysqli->prepare` method so I would have to call the parent's prepare to get my object. Thanks again. stackoverflow FTW
Dave
A: 

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);
}
Florian