Page 1 of 1

default values for function argument array

Posted: Fri Nov 05, 2010 4:31 pm
by zensys
I want to pass an array of parameters to the parent constructor where I have default values set for all three arguments. When I leave out argument 2, expecting that the default value 'bar2' is taken, a var_dump of $options only gives bar1 and bar3 but no bar2, not even null:

Code: Select all

class My_SubClass extends My_Class
{
	private static $_options = array(
			'foo1'  => 'bar1',
			'foo3' => 'bar3'
                  );
   
	public function __construct()
	{
	    parent::__construct(self::$_options);
        }
}

class My_Class
{
      public function __construct($options = array('foo1'  => null , 'foo2'  => 'bar2', 'foo3'  => null)) 
      { 
            // method
      }
      // class declaration continued
}

Any suggestions as how to get a default value for foo2?

Re: default values for function argument array

Posted: Fri Nov 05, 2010 4:34 pm
by requinix
Don't use an array?

Code: Select all

public function __construct($foo1 = null, $foo2 = 'bar2', $foo3 = null)