如何使用可选参数创建构造函数? [英] How to create constructor with optional parameters?

查看:128
本文介绍了如何使用可选参数创建构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有__construct($参数)

pre $ 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->电子邮件= $ user->电子邮件;
$ this->密码= $ user->密码;
$ 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;






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

  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;

}

但是有些错误,我得到这样的错误:

 致命错误:无法在/Applications/MAMP/htdocs/xxx/Application/User.php上重新声明User :: __ construct()第33行

如何使用两个带参数且不带参数的构造函数?

解决方案

非常感谢您的回答。

  public function __construct($ nick){
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->电子邮件= $ user->电子邮件;
$ this->密码= $ user->密码;
$ 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;
}

}

有效!


I have __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; 

}

And now I want to have a default constructor without parameters like this:

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; 

}

But something is wrong and I get error like this:

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

How can I have two constructors with parameters and without parameters at the same time?

解决方案

Thank you very much for your answer. I have solved the problem with my way.

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

}

It works!

这篇关于如何使用可选参数创建构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆