You could reorder the parameters based upon your experience of the likelihood that you are not going to override the defaults.
PHP Code:
function resize( $background = 'White', $text = 'Watermark', $width = 600, $height = 600);
{
// Resize code
}
resize('gray');
OR,
hardcode the defaults in the function, and leave the param default as NULL (yuk)
PHP Code:
function resize( $width = NULL, $height = NULL, $background = 'White', $text = 'Watermark');
{
if( $width===NULL ) $width = 800;
if( $height===NULL ) $height = 800;
// Resize code
}
resize(NULL, NULL, 'gray');
The benefits of these 2 is that your IDE may well prompt you to remind you of the order and help you fill them in as soon as it sees you write resize( ...
OR pass in an array of values and do say, a ternary check otherwise use a hardcoded default.
PHP Code:
function resize( $params );
{
$background = (isset($params['background'])) ? $params['background'] : 'white';
$background = (isset($params['height'])) ? $params['height'] : 800;
$background = (isset($params['width'])) ? $params['width'] : 800;
// Resize code
}
$params = array('background' => "gray");
resize( $params );
This has the drawback that you have to remember the values names but not the order. It is however useful if your input is coming from a form, say, which is already in a post or get array - simply unset() the form elements you do NOT want to send to the resize - or the params array can be an sql result as an array.
PHP Code:
<?php
// if this is spoofing your POST input ...
$_POST['background'] = 'green';
$_POST['height'] = '500';
$_POST['width'] = '500';
$_POST['submit'] = 'yes';
unset( $_POST['submit']);// ko this one
resize( $_POST );
This is one of those areas that OOP might excel if you wanted to load even more smarts into it.
PHP Code:
$i = new Resizer($path_to_file);
$i->setWatermark('My text');
$i->setSize(300);
// where because only one size is defined, a square can be assumed.
This carries much of the IDE benefits too.
Bookmarks