2015-02-08 97 views
3

我已经__construct($参数)如何使用可选参数创建构造函数?

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

虽然你可能在这两年后没有工作,但你在这段代码中有一个明显的SQL注入漏洞 – Casey 2017-07-27 19:23:41

回答

0

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

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

} 

它的工作原理!

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

相关问题