2014-10-17 70 views
2

我有一个小问题,只是为了教育目的...Zend框架 - PDO源代码

我被挖成Zend Framework的代码,以便了解它是如何工作的“由内而外”,我停在这小小的一段代码:(他们从PDO实施bindParam的()):

/** 
* Binds a parameter to the specified variable name. 
* 
* @param mixed $parameter Name the parameter, either integer or string. 
* @param mixed $variable Reference to PHP variable containing the value. 
* @param mixed $type  OPTIONAL Datatype of SQL parameter. 
* @param mixed $length OPTIONAL Length of SQL parameter. 
* @param mixed $options OPTIONAL Other options. 
* @return bool 
*/ 
public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null) 
{ 
    if (!is_int($parameter) && !is_string($parameter)) { 
     /** 
     * @see Zend_Db_Statement_Exception 
     */ 
     require_once 'Zend/Db/Statement/Exception.php'; 
     throw new Zend_Db_Statement_Exception('Invalid bind-variable position'); 
    } 

    $position = null; 
    if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) { 
     if ($intval >= 1 || $intval <= count($this->_sqlParam)) { 
      $position = $intval; 
     } 
    } else if ($this->_adapter->supportsParameters('named')) { 
     if ($parameter[0] != ':') { 
      $parameter = ':' . $parameter; 
     } 
     if (in_array($parameter, $this->_sqlParam) !== false) { 
      $position = $parameter; 
     } 
    } 

    if ($position === null) { 
     /** 
     * @see Zend_Db_Statement_Exception 
     */ 
     require_once 'Zend/Db/Statement/Exception.php'; 
     throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'"); 
    } 

    // Finally we are assured that $position is valid 
    $this->_bindParam[$position] =& $variable; 
    return $this->_bindParam($position, $variable, $type, $length, $options); 
} 

,我不明白的东西,是在函数结束时,他们返回

return $this->_bindParam($position, $variable, $type, $length, $options) 

这是声明为一个数组...

 /** 
    * Query parameter bindings; covers bindParam() and bindValue(). 
    * 
    * @var array 
    */ 
    protected $_bindParam = array(); 

他们如何返回一个数组并传递参数给它?

而且,我能找到的任何条件来停止递归...

你可以找到链接,这里的文件:

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Db/Statement.php

+0

运行代码和'var_dump'值,看看它们是什么。 – 2014-10-17 12:45:15

回答

1

Zend_Db_Statement是一个抽象类。
除非我弄错了,否则所有从Zend_Db_Statement继承的类声明一个_bindParam()方法。

例如Zend_Db_Statement_Pdo

class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggregate 
{ 
... 

    protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null) 
    { 
... 
    } 
} 
+0

对我而言,你是对的! 我应该看起来更仔细:) – user1506157 2014-10-17 13:07:24

+0

这是一个很好的问题,代码不是很清楚:) – doydoy44 2014-10-17 13:10:40