views:

383

answers:

4

I have two constructor :

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

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

But this is not working? What's wrong, isn't supposed to be OO at this version of PHP? (I just migrate from PHP4)

+3  A: 

PHP5 doesn't allow overloaded constructor.

Alternativly you can use function to set or you can use this trick (found at EE):

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

If you have a good reason to want to keep the function arguments in that order, do something like this:

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'd strongly recommend Daok's answer over this though.

Ant P.
+2  A: 

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

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

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

Note the confusion with the variable names in the example above! A better use of this "feature" in PHP would look like this, but you would need to update each function call in you application:

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

For clean-ness i would prefer the second one.

Kaii
A: 

You could use the func_get_args to count the number of passed arguments then match that up to a 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'