构造方法返回完整的类 Object,即使我调用其他函数也是如此


Construct method returning the complete class Object even when I call some other function

首先是我的类代码,

class Gateway extends Gateways
{
public function __construct($id = false)
{
    parent::__construct();
    if($id) return $this->getById($id);
}
public function getById($id)
{
    $this->gateway_select->columns($this->cols)->where(array($this->columns['ID'] => $id, $this->columns['IS_ACTIVE'] => 1));
    return $this->_fetch();
}
protected function _fetch()
{
    $this->gateway_select->limit(1);
    //echo $this->sql->getSqlStringForSqlObject($this->gateway_select);
    $result = $this->sql->prepareStatementForSqlObject($this->gateway_select)->execute()->current();
    return $result;
}

现在,当我在其他文件中调用此代码时

$gatewayRow = new Gateway($gatewayId);
print_r($gatewayRow);
exit;

输出是网关类对象,而不是从getById($id)方法获取的数据库行,但据我所知,返回的应该是数据库行。

当我单独调用getById()方法时,就像$gateway->setById($id);它的工作原理一样,所以它与__construct()有关

父::__construct代码

public function __construct()
{
    $this->db = TechMediaStudios::dbInstance();
    $this->sql = TechMediaStudios::sqlInstance();
    $this->gateway_select = $this->sql->select()->from($this->table);
    $this->_getColumns();
}

你不能从构造函数 period return任何东西。构造函数只能设置对象或抛出异常,它不能返回任何内容。无论你在哪里写new Foo,结果将是Foo的对象实例,没有别的。这不能被覆盖。