php - 如何创建带有可选参数的构造函数?

标签 php function oop

<分区>

我有 __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

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

最佳答案

  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)构造函数:

    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 - 如何创建带有可选参数的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395855/

相关文章:

php删除相同内容(不同: mysql key) in a table

php - PHP 服务器设置中的什么会导致 $_REQUEST 为空?

PHP如何显示多个数组值

c - 我的函数填充字符串值

jquery - 如何在没有触发器的情况下延迟函数的工作(文档准备好)

java - 对于(嵌套)类来说,简单到什么程度算太简单?

delphi - 我可以在抽象基类中保存任何数据成员吗?

java - 如何使用 CompareTo 对字母表进行排序

php - 正则表达式 php 和文本文件

javascript 使用 &lt;input type ="button"...> 而不是 <button ...>