default values for function argument array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zensys
Forum Newbie
Posts: 9
Joined: Mon May 31, 2010 5:16 am

default values for function argument array

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: default values for function argument array

Post by requinix »

Don't use an array?

Code: Select all

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