PHP Class construct with three optional parameters but one required? PHP Class construct with three optional parameters but one required? php php

PHP Class construct with three optional parameters but one required?


You could use an array to address a specific parameter:

function __construct($param) {    $id = null;    $email = null;    $username = null;    if (is_int($param)) {        // numerical ID was given        $id = $param;    } elseif (is_array($param)) {        if (isset($param['id'])) {            $id = $param['id'];        }        if (isset($param['email'])) {            $email = $param['email'];        }        if (isset($param['username'])) {            $username = $param['username'];        }    }}

And how you can use this:

// IDnew User(12345);// emailnew User(array('email'=>'user@example.com'));// usernamenew User(array('username'=>'John Doe'));// multiplenew User(array('username'=>'John Doe', 'email'=>'user@example.com'));