SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)

    Decorating Stmt for binding

    What would be the best way to decorate a stmt object to be able to use binding? The only thing I have thought of is a nasty eval. Any other ideas?

    Code:
    interface IBindable {
      public function bind_param();
    }
    
    class StmtDecorator implements IBindable {
      private $_stmt;
    
      public function __construct($pStmt) {
        $this->_stmt = $pStmt;
      }
    
      public function bind_param() {
        $this->_stmt->bind_param(???????);
      }
    }

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    It seems as if this isn't even possible without eval unless you actually can define the parameters for the method because the variables need to be passed into the bind_param method of the stmt object or am I wrong?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Code:
    interface IBindUsers {
      public function bind_param($pType,&$pFirstName,&$pLastName);
    }
    
    class StmtDecorator implements IBindable {
      private $_stmt;
    
      public function __construct($pStmt) {
        $this->_stmt = $pStmt;
      }
    
      public function bind_param($pType,&$pFirstName,&$pLastName) {
        $this->_stmt->bind_param($pType,$pFirstName,$pLastName);
      }
    }
    It seems as if that would be the only way to do this. Creating separate classes or method that have a set of predefined parameters which than be passed via reference. Any other ideas (without using eval) ?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    eval implementation I was referring to (which is fairly dirty).

    Code:
    interface IBindUsers {
      public function bind_param();
    }
    
    class StmtDecorator implements IBindable {
      private $_stmt;
    
      public function __construct($pStmt) {
        $this->_stmt = $pStmt;
      }
    
      public function bind_param() {
      
    	$args = func_get_args();
    	$typeStr = array_shift($args);
    	$newArgs = array();
    	$par = array();
    
    	for($i=0;$i<count($args):$i++) {
    		$newArgs[] = '$arg_'.$i.' = $args['.$i.']';
    		$par[] = '$arg_'.$i;
    	}
    
    	$evStr = implode(';',$newArgs);
    	eval($evStr);
    	$evPar = preg_replace('/,$/','',implode(',',$par));
    
    	$evBind = '$this->_stmt->bind_param($typeStr,'.$evPar.');';
    	eval($evBind);
      }
    }

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly are you trying to achieve?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    A common interface between mysql,mysqli and pdo. So even though the generic mysql connection doesn't implement binding all these connections would interchangable because they all would have a common interface. The part where I'm stuck is binding params, values and columns. Obviously, the mysql connection would just call the mysql_query() in its binding method, but as for the others I'm not sure how to pass the variables along without using eval to create a parameter string.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your problem is that you want to create a method with variable number of arguments, taken by reference? Like in mysqli_stmt_bind_param?
    The only way I can think of, is to create a lot of dummy-arguments - enough to cover all realistic cases - and then give them a default value. Eg.:
    PHP Code:
    function foo(&$var1, &$var2 null, &$var3 null, &$var4 null, &$var5 null, &$var6 null, &$var7 null, &$var8 null) {
    ...

    The problem is that for a variable to be by-reference, it must be declared so in the method signature.

    That said, I think that the "bind" feature of the various database-api's is a hideous mal-feature. If it was me, I would simply not include it. Functions should not alter their input-parameters - they should return any values that is needed. References has potential for some very incomprehensible code.


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
  •