Php 如何使用可选参数创建构造函数?

Php 如何使用可选参数创建构造函数?,php,function,oop,Php,Function,Oop,我有_构造($parameter) 现在我想要一个没有如下参数的默认构造函数: public function __construct() { $this->id = NULL; $this->nick = NULL; $this->email = NULL; $this->password = NULL; $this->birthDay = NULL; $this->sex = NULL; $this

我有_构造($parameter)

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

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
但有些地方出了问题,我得到了如下错误:

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
我怎么能同时有两个带参数和不带参数的构造函数

  • PHP中没有重载;不能有名称相同但参数不同的方法

  • 因为您的所有属性都默认为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; 
        }
    }
    
  • 上述代码的另一种选择是使用多个静态构造函数方法,并使用私有构造函数:

    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();
    }
    

  • 有关详细信息,请查看此答案:

    非常感谢您的回答。我用我的方法解决了这个问题

    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;
        }
    
    }
    

    它起作用了

    虽然两年后你可能还没有开始研究这个问题,但这段代码中有一个明显的SQL注入错误。我认为这不是一个好的实现。我认为你错过了@TimCooper提出的可选的实际论点,这里是线索
    public function\u构造($nick=null)
    注意,在PHP中,构造函数不允许抛出任何
    可丢弃的
    s,例如
    异常
    s。它只会使PHP脚本的执行崩溃。通常PHP中的数据库查询允许抛出
    异常
    s。但我不确定在这种情况下这个App::东西是什么。