Zend框架 - PDO源代码

时间:2014-10-17 12:37:24

标签: php zend-framework 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

1 个答案:

答案 0 :(得分: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)
    {
...
    }
}