【问题标题】:How to create constructor with optional parameters? [duplicate]如何创建带有可选参数的构造函数? [复制]
【发布时间】:2015-04-08 08:54:28
【问题描述】:

我有 __construct($parameter)

public function __construct($nick) {
    $query = "SELECT * FROM Users WHERE nick='".$nick."'";
    $result = App::runQuery($query);
    $user = $result->fetch_object();

    $this->id = $user->id;
    $this->nick = $user->nick;
    $this->email = $user->email;
    $this->password = $user->password;
    $this->birthDay = $user->birthDay;
    $this->sex = $user->sex;
    $this->about = $user->about;
    $this->city = $user->city;
    $this->photo = $user->photo;
    $this->following = $user->following;
    $this->followers = $user->followers;
    $this->statu = $user->statu; 

}

现在我想要一个没有参数的默认构造函数:

public function __construct() {

    $this->id = NULL;
    $this->nick = NULL;
    $this->email = NULL;
    $this->password = NULL;
    $this->birthDay = NULL;
    $this->sex = NULL;
    $this->about = NULL;
    $this->city = NULL;
    $this->photo = NULL;
    $this->following = NULL;
    $this->followers = NULL;
    $this->statu = NULL; 

}

但是出了点问题,我收到这样的错误:

Fatal error: Cannot redeclare User::__construct() in /Applications/MAMP/htdocs/xxx/Application/User.php on line 33

如何同时拥有两个带参数和不带参数的构造函数?

【问题讨论】:

  • 虽然两年后您可能不再从事此工作,但此代码中存在明显的 SQL 注入错误

标签: php function oop


【解决方案1】:
  1. PHP 中没有重载;你不能有同名但不同参数的方法

  2. 由于您的所有属性都将默认为 null,因此没有理由自己做。我建议执行以下操作:

    public function __construct($nick = null) {
        if ($nick != null) {
            $query = "SELECT * FROM Users WHERE nick='".$nick."'";
            $result = App::runQuery($query);
            $user = $result->fetch_object();
    
            $this->id = $user->id;
            $this->nick = $user->nick;
            $this->email = $user->email;
            $this->password = $user->password;
            $this->birthDay = $user->birthDay;
            $this->sex = $user->sex;
            $this->about = $user->about;
            $this->city = $user->city;
            $this->photo = $user->photo;
            $this->following = $user->following;
            $this->followers = $user->followers;
            $this->statu = $user->statu; 
        }
    }
    
  3. 上述代码的替代方案是拥有多个静态构造函数方法,并使用私有构造函数:

    private function __construct() {
    }
    
    public static function createFromNick($nick) {
        $self = new self();
    
        $query = "SELECT * FROM Users WHERE nick='".$nick."'";
        $result = App::runQuery($query);
        $user = $result->fetch_object();
    
        $self->id = $user->id;
        $self->nick = $user->nick;
        $self->email = $user->email;
        $self->password = $user->password;
        $self->birthDay = $user->birthDay;
        $self->sex = $user->sex;
        $self->about = $user->about;
        $self->city = $user->city;
        $self->photo = $user->photo;
        $self->following = $user->following;
        $self->followers = $user->followers;
        $self->statu = $user->statu;
    
        return $self;
    }
    
    public static function createEmpty() {
        return new self();
    }
    

【讨论】:

  • 请注意,在 PHP 构造函数中不允许抛出任何Throwables,例如Exceptions。它只会使 PHP 脚本的执行崩溃。通常允许 PHP 中的数据库查询抛出Exceptions。但我不确定这个 App:: 在这种情况下是什么。
【解决方案2】:
public function __construct() {
    // allocate your stuff
}

public static function withRow( array $row ) {
    $instance = new self();
    $instance->fill( $row );
    return $instance;
}

protected function fill( array $row ) {
    // fill all properties from array
}

有关详细信息,请查看此答案:Best way to do multiple constructors in PHP

【讨论】:

    【解决方案3】:

    非常感谢您的回答。我已经用我的方式解决了问题。

    public function __construct($nick) {
        if($nick != NULL) {//for user 
            $query = "SELECT * FROM Users WHERE nick='".$nick."'";
            $result = App::runQuery($query);
            $user = $result->fetch_object();
    
            $this->id = $user->id;
            $this->nick = $user->nick;
            $this->email = $user->email;
            $this->password = $user->password;
            $this->birthDay = $user->birthDay;
            $this->sex = $user->sex;
            $this->about = $user->about;
            $this->city = $user->city;
            $this->photo = $user->photo;
            $this->following = $user->following;
            $this->followers = $user->followers;
            $this->statu = $user->statu; 
        }else {//for null
            $this->id = NULL;
            $this->nick = NULL;
            $this->email = NULL;
            $this->password = NULL;
            $this->birthDay = NULL;
            $this->sex = NULL;
            $this->about = NULL;
            $this->city = NULL;
            $this->photo = NULL;
            $this->following = NULL;
            $this->followers = NULL;
            $this->statu = NULL;
        }
    
    }
    

    有效!

    【讨论】:

    • 我认为这不是一个好的实现
    • 我认为您错过了@TimCooper 解决的可选参数的实际要点,这是线索public function __construct($nick = null)
    • PHP > 7.4 应该是public function __construct($nick = null)
    • 您的代码有效,因为它只有一个参数。 else 部分也可以省略,因为它是属性的默认值。对于需要零个或多个参数的构造函数,我只能考虑使用数组来处理它。然后我可以检查参数是否为数组,如果是,则检查数组中的条目数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多