Re: PHP 5.1
| From: | Ron Korving | Date: | Tue, 14 Jun 2005 12:30:49 +0000 |
| Subject: | Re: PHP 5.1 | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-16660@lists.php.net to get a copy of this message | ||
But how about this..:
Let's not implement functions that do what you want to achieve, but extend
PHP in whatever way necessary that would enable you to write such a function
yourself?
Say for example that prepending @ to function parameters would make it
possible to call the function with an unset variable. Then you could write
ifsetor() yourself:
function ifsetor(@$var, $default)
{
return (isset($var)) ? $var : $default;
}
Someone else might prefer call by reference:
function ifsetor(@&$var, $default)
{
if (!isset($var)) $var = $default;
}
And someone else might prefer using empty() :
function notemptyor(@$var, $default)
{
return (empty($var)) ? $default : $var;
}
So what I would prefer is a syntax extension of some sort. The @ is just an
example of course, and I'm sure it's not the prettiest way to deal with the
feature, but I think implementing such a thing would be best for everybody.
This way everybody can implement the ifsetor() feature in whatever way they
please. It would end the whole ifsetor() discussion in this list anyway ;)
Ron
"Hartmut Holzgraefe" <hartmut@php.net> wrote in message
news:42AEC486.3080408@php.net...
> M. Sokolewicz wrote:
> >> $x = (isset($ANY_var) ? $ANY_var : 'Default Value');
> >> by
> >> $x = ifsetor($ANY_var, 'Default Value');
> >>
> > I must say I fully agree; I don't see any use in putting extra functions
> > in the PHP namespace just because people don't want to type a couple of
> > extra characters.
>
> it is not "a few extra characters", its "needless duplication of
characters",
> and the number of characters multiplies with the number of uses of the
> construct
>
> please keep in mind that $ANY_var can be a variable name of any length
> or an element of a multidimensional array. this can leed to unreadably
long
> statements, hard to track down copy errors and is a maintainance nichtmare
> as you always have to change things in two places at the same time
>
> How quick can you spot the error in the following assignment?
> And if you didn't know the context: how long would it take you
> to find out what it does?
>
> $x =
> (isset($config['general']['foo']['bar']['somelist']) ?
$config['general']['foo']['bar']['sonelist'] :
"default");
>
> On the other hand the meaning of
>
> $x =
> ifsetor($config['general']['foo']['bar']['somelist'],
> "default");
>
> is pretty clear, only about half as long and way less error prone.
>
> I don't really like the name 'ifsetor' but i *love* the concept and given
> that there is no proper way to implement this function in user space
> i'm perfectly in favor of its addition.