0

阿罗哈:)

是否可以有一个带有引用作为参数的构造函数,然后从另一个类中使用它?

    Class 1 named ABC
    function __construct( $table, $key, &$db ){

    $this->_tbl     = $table;
    $this->_tbl_key = $key;
    $this->_db      =& $db;
    $this->_pkey = $key;
}

接下来我想在 XYZ 中使用 ABC。像这样:

    $ABC= new ABC("dummytable", "id");

但显然这缺少一个参数,当我输入一个参数时,它返回一个关于参考的错误..

我该如何解决这个问题?

谢谢 :)

4

1 回答 1

2

如果$db是对象,则不要将其作为参考传递。如果参数是可选的,则给它一个默认值。

function __construct( $table, $key, $db = null){
    $this->_tbl     = $table;
    $this->_tbl_key = $key;
    $this->_db      = $db;
    $this->_pkey = $key;
}
于 2012-08-24T08:16:35.293 回答