How can I load a constructor in Php5?

I have two constructors:

function clsUsagerEmailUserName($nickName, $email)
{
        $this->nickName = $nickName;
        $this->email = $email;               
}

function clsUsagerEmailUserName($email)
{
        $this->email = $email;               
}

      

But it doesn't work? What's wrong, shouldn't be OO in this PHP version? (I am just migrating from PHP4)

0


source to share


4 answers


PHP5 does not allow overloaded constructor.

Alternatively you can use a function to install, or you can use this trick (found in EE):



function __construct ($var1, $var2 = null)
{
   if (isset($var2))
   {
      //Do one thing
   }
   else
   {
      //Do another
   }
}

      

+2




If you have a good reason to leave the function arguments in that order, follow these steps:

function __construct()
{
    switch ( func_num_args() ) {
        case 1:
            $this->email = func_get_arg(0);
        break;
        case 2:
            $this->nickName = func_get_arg(0);
            $this->email = func_get_arg(1);
        break;
        // [...]
        default:
            throw new Exception('Wrong number of values passed to constructor');
    }
}

      



I would highly recommend Daok's answer to this though.

+3




PHP supports object oriented constructs in newer versions, but function overloading is not part of the object oriented paradigm.

As someone else said, PHP does not support overloading functions. In PHP, you can define "default values" for function parameters. Your function might look like this, with your expected behavior:

function clsUsagerEmailUserName($nickName, $email = NULL)
{    
    if ($email <> NULL)
    {
        $this->nickName = $nickName;
        $this->email = $eMail;
    }
    else
    {
        $this->email = $nickName;
    }
}

      

Notice the confusion with variable names in the above example! The best use of this "function" in PHP would look like this, but you will need to update every function call in your application:

function clsUsagerEmailUserName($email, $nickName = NULL)
{
    $this->email = $email;
    if ($nickName <> NULL)
        $this->nickName = $nickName;
}

      

For cleanliness, I would prefer the latter.

+2


source


You can use func_get_args to count the number of arguments passed and then up to this method.

abstract class AlmostPolymorphicObject {
  public function __construct() {
    $args = func_get_args();
    $argsCount = count($args);
    $callback = array($this, '_construct'.$argsCount);
    if (!is_callable($callback)) {
      throw new Exception('no valid constructor for param count '.$argsCount);
    }
    return call_user_func_array($callback, $args);
  }
}

class FooBar extends AlmostPolymorphicObject {
  private function _construct0() {
  }

  private function _construct1($var1) {
  }

  private function _construct2($var1, $var2) {
  }
}

$var = new FooBar(); // would run _construct0
$var = new FooBar('var'); // would run _construct1
$var = new FooBar('var','var'); // would run _construct2
$var = new FooBar('var','var', 'asdasd'); // would throw error 'no valid constructor for param count 3'

      

+1


source







All Articles