Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create constructor with optional parameters? [duplicate]

Tags:

function

oop

php

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?

like image 602
Mehmet Avatar asked Feb 08 '15 15:02

Mehmet


1 Answers

  1. There is no overloading in PHP; you cannot have methods with the same names but different parameters

  2. Since all of your properties will default to null, there's no reason to do it yourself. I would suggest doing the following:

    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. An alternative to the above code is to have multiple, static constructor methods, and use a private constructor:

    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();
    }
    
like image 172
Tim Cooper Avatar answered Oct 19 '22 06:10

Tim Cooper