PHP arrays are powerful. Similar to highelf I would use an array.
However, I would do a number of things differently to highelf.
- Don't create public properties from the array you receive (especially using unknown array keys).
- Check the values that you receive.
As edorian said, there is no type hinting, but you can enforce the required parameters manually. The advantages of this approach are:
- Its easy to remember the number of parameters (1).
- The order of the parameters is no longer important. (No need to worry about keeping a consistent order of parameters, although alphabetically works well.)
- The calling code is more readable as the parameters are named.
- It is very hard to accidentally set the width to the height even though the type is the same. (This is easily overlooked by type hinting).
So here is what I would do:
class OpenIdProvider
{
protected $path;
protected $setup;
public function __construct(Array $setup)
{
// Ensure the setup array has keys ready for checking the parameters.
// I decided to default some parameters (height, highlighting, something).
$this->setup = array_merge(
array('height' => 68,
'highlighting' => 'openid_highlight',
'info' => NULL,
'name' => NULL,
'path' => NULL,
'something' => 6,
'width' => NULL),
$setup);
// The following checks may look lengthy, but it avoids the creation of
// the OpenIdProviderOptions class that seems to do very little. Also,
// these would appear in that class to check the constructor values it
// received properly.
if (!is_array($this->setup['info']))
{
throw new InvalidArgumentException(
__METHOD__ . ' requires info as array');
}
if (!is_string($this->setup['name']))
{
throw new InvalidArgumentException(
__METHOD__ . ' requires name as string');
}
if (!is_string($this->setup['path']))
{
throw new InvalidArgumentException(
__METHOD__ . ' requires path as string');
}
if (!is_int($this->setup['width']))
{
throw new InvalidArgumentException(
__METHOD__ . ' requires width as int');
}
// If you use a setup parameter a lot you might want to refer to it from
// this, rather than having to go via setup.
$this->path =& $this->setup['path'];
}
public function internalFunction()
{
// Use the parameters like so.
echo $this->setup['width'];
echo $this->path;
}
}
// The call is very easy.
$op = new OpenIdProvider(
array('info' => $info,
'name' => $name,
'path' => $imgPath . $name . $ext,
'width' => 108));