Looks like PHP won’t get named parameters on version 6. And, well, I am not the only one that want it 🙂 . It’s a feature of Python that I love. Why? Consider the following situation:
You’re making a function/method to return a string with HTML code for an input field.
function inputField($name, $maxlength = 50, $size = 20)
{
return " ";
}
Later on, you need to put a label attribute on your inputText()’s. You can change every function call to pass this parameter or making it optional at the function definition, which is probably desired because it’s easier and faster than changing dozens of lines of code (and coming up with label names):
function inputField($name, $maxlength = 50, $size = 20, $label = '')
{
$html = "
Sometime later, you need to put javascript events on your input. You can change the function definition again, and it works. But then you’ll need to add a value attribute, or disabled and the many other possible input attributes. That’s impractical on a daily basis, and we’re just talking about input=”text”, not mentioning selects, textareas, etc.
AND you have to remember the exact order of the parameters. You will write code that looks like this:
function inputField($name, $maxlength = 50, $size = 20, $label = '', $value = '', $javascript = '', $disabled = '')
{
// bunch of if's here
}
echo inputField($name, 50, 20, '', 'a_value', 'disabled');
echo inputField($name, 50, 20, $name, 'a_value', 'onclick=js()', '');
echo inputField($name, 50, 20, '', '', '', 'disabled');
These are not pretty function calls at all 🙁
Named parameters could be a solution. You just need to test if the parameter is present, and use it. But PHP doesn’t support them… There are alternatives, like using associative arrays or eval()’ing a string.
But here’s a cleaner, more readable, and expansible way to do it:
class named
{
}
$named = new named();
function foo()
{
global $named;
echo "foo() called\n";
if (isset($named->var)) {
echo "\$var->named passed as a parameter with $named->var as value\n";
}
}
echo foo();
echo foo($named->var = "test");
/*
Outputs:
foo() called
foo() called
$var->named passed as a parameter with test as value
*/
Below is the inputText() function rewritten to use this technique. But to make it more useful we will need to perform a clean-up on $named after we use it, because otherwise all its attributes will be carried between function calls (that’s the effect of global $named), and we don’t want that 🙂
For this example, since every inputText() needs to have a name attribute, I am making it a required parameter at the function definition.
class named
{
public function clean()
{
foreach ($this as $key => $value) {
unset($this->$key);
}
}
}
$named = new named();
function inputText($name)
{
global $named;
$html = "maxlength)) {
$html .= "maxlength=\"$named->maxlength\" ";
}
if (isset($named->disabled)) {
$html .= "disabled=\"$named->disabled\" ";
}
$html .= " />\n";
$named->clean();
return $html;
}
echo inputText('input_name');
echo inputText('input_name', $named->maxlength = 5);
echo inputText('input_name', $named->disabled = "disabled");
echo inputText('input_name', $named->disabled = "disabled", $named->maxlength = 5);
// Order doesn't matter.
echo inputText('input_name', $named->maxlength = 5, $named->disabled = "disabled");
/*
Outputs:
*/
All we needed were two more lines of code (global $named and $named->clean())
The above class could be further customized with some Overloading, providing for example default values, or even reacting to method calls. Heck, since I am learning what’s new in PHP 5 for just a couple days, I bet there’s a way to automate the clean() method, but couldn’t come up with a way to do it, for now.
Hope you liked 🙂
Leave a Reply
You must be logged in to post a comment.