SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extending method, passing by reference

    I am making a class extending the mysqli_stmt class. But I am having problems with the bind_param method. In these situations where I need to override a native PHP function that can handle unlimited amount of parameters I use this code:

    Code:
    <?php
    public function bind_param($types){
    
      if( call_user_func_array(array($this, 'parent::'.__FUNCTION__), $args) 
           === false ){
                 throw new  DBQueryException($this->error,$this->errno,$this->sqlstate,$this->query);
      }		
    }
    ?>
    But the bind_params function requires the parameters after $types to be passed by reference. How can I do this?

  2. #2
    SitePoint Member
    Join Date
    Jul 2006
    Location
    Prague, Czech republic
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I gues it is not possible this way. If you need to pass unknown amount of variables by reference, you can use an array:
    PHP Code:
    function bind_param($types$args) {
        
    call_user_func_array(array("parent""bind_param"), $args);
    }

    // usage:
    $stmt->bind_param("iii", array(&$param1, &$param2, &$param3)); 
    Ugly hack . Or you can pass always only one parameter and to provide so called "fluent interface":
    PHP Code:
    function bind_param($type, &$param) {
        
    call_user_func_array(array("parent""bind_param"), array(&$param));
        return 
    $this;
    }

    // usage:
    $stmt->bind_param("i"$param1)
        ->
    bind_param("i"$param2)
        ->
    bind_param("i"$param3); 
    Or you can change the behavior and pass them by value...

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem here is that I am extending the native mysqli function bind_param, and that requires multiple parameters to be passed by ref.

    http://php.net/manual/en/function.my...bind-param.php

  4. #4
    SitePoint Zealot Mau's Avatar
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps one of the rare appropriate uses of eval()?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since passing by reference is defined in the message signature, and not in the calling code, there is no way to do this.

    Maybe rather than extending mysqli_stmt(), perhaps you should decorate it. I found myself having to do this recently to with a Statement class. I wanted to be able to foreach() a PDOStatement object with PDO::FETCH_CLASS and PDO::FETCH_OBJECT. But those fetch modes only work with the fetch*() methods, so I ended up wrapping a PDOStatement object in an Iterator.
    Zealotry is contingent upon 100 posts and addiction 200?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •