Re: what happened to that new isset() like language

php.internals

James Crumpton

20 years ago
Andi Gutmans wrote:
> I don't think it's a matter of giving the engine a try. I think we first need > to make a decision what the best way to go is and then we can discuss > implementation if/what is possible. Once 5.0.0 is out I'm going to have more > time look into this.
What's the word on this? Some of the names given were: issetor ifsetor ifset ifnull coalesce I'm actually kind of partial to: $a = $foo || $bar; $a = $foo || $bar || $xyz || $etc; Or better yet (or maybe in addition too): $a['foo'] ||= 'bar'; // if set do nothing, otherwise assign 'bar' -james

sebastian

20 years ago
Wow, it'll be just like perl! this is so great. On 10/28/05, James Crumpton <james@safesearching.com> wrote:
> Andi Gutmans wrote: > > I don't think it's a matter of giving the engine a try. I think we first need > > to make a decision what the best way to go is and then we can discuss > > implementation if/what is possible. Once 5.0.0 is out I'm going to have more > > time look into this. > > What's the word on this? > > Some of the names given were: > issetor > ifsetor > ifset > ifnull > coalesce > > I'm actually kind of partial to: > > $a = $foo || $bar; > $a = $foo || $bar || $xyz || $etc; > > Or better yet (or maybe in addition too): > > $a['foo'] ||= 'bar'; // if set do nothing, otherwise assign 'bar' > > > -james > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
-- ------------- buddhahead@gmail.com

Edin Kadribasic

20 years ago
If we're going to add a new language construct I believe that it should be readable and not some perl-like thingie. SQL has coalesce() functions which returns returns first non-null argument of any number of arguments. PHP version could return a first set argument: $lang = coalesce($_COOKIE['lang'], $user_settings['lang'], 'en'); It shouldn't be too difficult to tell what this one does. Edin Sebastian wrote:

Marcus Börger

20 years ago
Hello Edin, on the other hand it is not doable....not without a major slowdown. Thus the idea of ifsetor($var, <expression>). Why the hell do we need to discuss this over and over again? The only way is this version. Be it with some cryptic operator like '?:' or some function name like 'ifsetor'. Either way our internals and the use of compiler compiler tools do not allow other stuff. And speaking of '?:' we turned that down because it does suggest '$a ? $a : <expr>' which is different from the functionality we actually want: 'isset($a) ? $a : <expr>'. And actually 'ifsetor' was only turned down because someone very very hard tried to find a way to turn it down and could only come up with saying its name is bad. Which i obviously absolutley disagree since its name is perfectly clear and does not lead to confusion. Becuase it suggests something like "IF something isSET than take it OR use the other". regards marcus Sunday, October 30, 2005, 1:18:53 AM, you wrote:
> If we're going to add a new language construct I believe that it should > be readable and not some perl-like thingie. SQL has coalesce() functions > which returns returns first non-null argument of any number of > arguments. PHP version could return a first set argument:
> $lang = coalesce($_COOKIE['lang'], $user_settings['lang'], 'en');
> It shouldn't be too difficult to tell what this one does.
> Edin
> Sebastian wrote: >> Wow, it'll be just like perl! this is so great. >> >> On 10/28/05, James Crumpton <james@safesearching.com> wrote: >>> Andi Gutmans wrote: >>> > I don't think it's a matter of giving the engine a try. I think we first need >>> > to make a decision what the best way to go is and then we can discuss >>> > implementation if/what is possible. Once 5.0.0 is out I'm going to have more >>> > time look into this. >>> >>> What's the word on this? >>> >>> Some of the names given were: >>> issetor >>> ifsetor >>> ifset >>> ifnull >>> coalesce >>> >>> I'm actually kind of partial to: >>> >>> $a = $foo || $bar; >>> $a = $foo || $bar || $xyz || $etc; >>> >>> Or better yet (or maybe in addition too): >>> >>> $a['foo'] ||= 'bar'; // if set do nothing, otherwise assign 'bar' >>> >>> >>> -james >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> >> >> >> -- >> >> >> >> ------------- >> buddhahead@gmail.com >>
Best regards, Marcus

Bob Silva

20 years ago
It should be a function and not a language construct IMHO. Either ifsetor or overload the isset statement (which may not be possible within the engine). If isset called with 1 argument, it returns a boolean, otherwise it returns the first valid variable. Instead of: $localvar = (isset($_REQUEST['globalvar'])) ? $_REQUEST['globalvar'] : "default value"; This makes perfect sense (to me at least): $localvar = isset($_REQUEST['globalvar'], "default value 1", ...); That covers the most common usage of the isset variable. Whether its possible in the engine is out of my range of knowledge. Bob Silva

Arpad Ray

20 years ago
Bob Silva wrote:
> This makes perfect sense (to me at least): > > $localvar = isset($_REQUEST['globalvar'], "default value 1", ...); >
The obvious problem there being that isset() currently takes multiple variables as arguments and checks that they're all set. From the manual: bool isset ( mixed var [, mixed var [, ...]] ) Arpad

Bob Silva

20 years ago
Whoops, geez, you use a language for 8 years and you'd think you know what it can do! Beyond that ifset is a nicer name than ifsetor, people can RTFM (like I obviously should have done) to figure out the functionality.

Sean Coates

20 years ago
Bob Silva wrote:
> It should be a function and not a language construct IMHO. Either ifsetor or > overload the isset statement (which may not be possible within the engine).
It _has_ to be a language construct, and not a function (otherwise, we'd get a notice when first using the variable). It could, however, be a language construct that LOOKS like a function. This is how isset() and empty() work. Also, if this was possible as a function, it could be solved in user-space, and we wouldn't be having this conversation. (not that I claim to be an internals expert.. if I'm wrong, feel free to correct me) To pitch in my 0.02 CAD: this functionality is valuable to me, but I don't care what we name it, or how it works. I could also live without it: isset() + ternary + redundant typing. S

Ron Korving

20 years ago
I'm a big fan of coalesce($param1, $param2, ..., $paramN) (or firstset()). The syntax allows for more than what ifsetor($var, $value) would do. Ifsetor() could be done in userspace, but I don't see how coalesce() could, because of the variable number of parameters. Ron "Sean Coates" <sean@caedmon.net> wrote in message news:43643EDF.1010301@caedmon.net...
> Bob Silva wrote: > > It should be a function and not a language construct IMHO. Either
ifsetor or
> > overload the isset statement (which may not be possible within the
engine).

Marian Kostadinov

20 years ago
Whatever the name is, I think the most important thing should be that second argument is not evaluated if the first one is set. And about the name - $a = $b ?? $c looks good. $a ??= $c is fine too because it is close to $a = $b || $c and $a ||= $c.. On 30/10/05, Ron Korving <r.korving@xit.nl> wrote:

Marcus Börger

20 years ago
Hello Ron, damn it! ifsetor or any equivalent cannot be done in userspace. If you don't get don't write here. marcus Sunday, October 30, 2005, 9:52:47 AM, you wrote:
> I'm a big fan of coalesce($param1, $param2, ..., $paramN) (or firstset()). > The syntax allows for more than what ifsetor($var, $value) would do. > Ifsetor() could be done in userspace, but I don't see how coalesce() could, > because of the variable number of parameters.
> Ron
> "Sean Coates" <sean@caedmon.net> wrote in message > news:43643EDF.1010301@caedmon.net... >> Bob Silva wrote: >> > It should be a function and not a language construct IMHO. Either > ifsetor or >> > overload the isset statement (which may not be possible within the > engine). >> >> It _has_ to be a language construct, and not a function (otherwise, we'd >> get a notice when first using the variable). >> >> It could, however, be a language construct that LOOKS like a function. >> This is how isset() and empty() work. >> >> Also, if this was possible as a function, it could be solved in >> user-space, and we wouldn't be having this conversation. >> >> (not that I claim to be an internals expert.. if I'm wrong, feel free to >> correct me) >> >> To pitch in my 0.02 CAD: this functionality is valuable to me, but I >> don't care what we name it, or how it works. I could also live without >> it: isset() + ternary + redundant typing. >> >> S
Best regards, Marcus

Ron Korving

20 years ago
Marcus, my point was that this simple function: function ifsetor(&$var, $value) { return (isset($var)) ? $var : $value; } can be done in userspace, and that a coalesce() like function will have the added benefit of a variable number of parameters, which as far as I know, cannot be done in userspace, because call-by-reference cannot be done using func_get_args(). That was all, take it easy :) Ron "Marcus Boerger" <helly@php.net> wrote in message news:10757231.20051030104756@marcus-boerger.de...
> Hello Ron, > > damn it! ifsetor or any equivalent cannot be done in userspace. If you > don't get don't write here. > > marcus > > Sunday, October 30, 2005, 9:52:47 AM, you wrote: > > > I'm a big fan of coalesce($param1, $param2, ..., $paramN) (or
firstset()).
> > The syntax allows for more than what ifsetor($var, $value) would do. > > Ifsetor() could be done in userspace, but I don't see how coalesce()
could,
> > because of the variable number of parameters. > > > Ron > > > > "Sean Coates" <sean@caedmon.net> wrote in message > > news:43643EDF.1010301@caedmon.net... > >> Bob Silva wrote: > >> > It should be a function and not a language construct IMHO. Either > > ifsetor or > >> > overload the isset statement (which may not be possible within the > > engine). > >> > >> It _has_ to be a language construct, and not a function (otherwise,
we'd
> >> get a notice when first using the variable). > >> > >> It could, however, be a language construct that LOOKS like a function. > >> This is how isset() and empty() work. > >> > >> Also, if this was possible as a function, it could be solved in > >> user-space, and we wouldn't be having this conversation. > >> > >> (not that I claim to be an internals expert.. if I'm wrong, feel free
to

Sara Golemon

20 years ago
> function ifsetor(&$var, $value) > { > return (isset($var)) ? $var : $value; > } > > can be done in userspace. >
Perhaps, but it won't do what you think, and certainly won't do what this thread has been discussing for over half a year.

Sven Fuchs

20 years ago
Hi Marcus,
> ifsetor or any equivalent cannot be done in userspace.
i agree here. Last time this topic has been discussed I've naively asked for opinions regarding a userland implementation. Afterwards the discussion suddenly ended. In the meantime I've tried to use it more often but found myself typing those $a = isset($arr[with][many][keys]) ? $arr[with][many][keys] : ... statements again because my userland ifsetor() simply doesn't do what I needed. I also agree with Marian on this:
> Whatever the name is, I think the most important thing should be > that second argument is not evaluated if the first one is set.
$b = ifsetor($a, do_expensive_operation()); After really having tried to use the userland ifsetor() discussed on this list some months ago, I believe that a more useful ifsetor() would: 1. not touch $a 2. not call do_expensive_opertion() when $a is set. Just my two cents to a bikeshed's color ;)
-- Sven

Jasper Bryant-Greene

20 years ago
On Sun, 2005-10-30 at 09:52 +0100, Ron Korving wrote:
> I'm a big fan of coalesce($param1, $param2, ..., $paramN) (or firstset()). > The syntax allows for more than what ifsetor($var, $value) would do. > Ifsetor() could be done in userspace, but I don't see how coalesce() could, > because of the variable number of parameters.
It's not so much the variable number of parameters as the fact that it has to receive a variable that may not be set. Using a user-space function will cause an E_NOTICE to be thrown every time the function is used, because it's receiving an unset variable. It needs to be a language construct. FWIW, I like the $a ?? $b and $a ??= $b idea.
-- Jasper Bryant-Greene General Manager Album Limited e: jasper@album.co.nz w: http://www.album.co.nz/ b: http://jbg.name/ p: 0800 4 ALBUM (0800 425 286) or +64 21 232 3303 a: PO Box 579, Christchurch 8015, New Zealand

Ron Korving

20 years ago
The function I used in my post does not generate an E_NOTICE, because a call-by-reference can be done with an unset variable (that can be set from the function). <?php error_reporting(E_ALL); function ifsetor(&$var, $value) { return (isset($var)) ? $var : $value; } echo ifsetor($bla, 'test'); ?> This simply prints 'test' and shows no errors. In fact, a var_dump() on $bla will show that it's become NULL. Never mind this whole ifsetor() in userspace issue, because it's not applicable anyway, but just for the record, this function does not generate an E_NOTICE. Ron "Jasper Bryant-Greene" <jasper@album.co.nz> wrote in message news:1130700022.3009.1.camel@jasper.local...
> On Sun, 2005-10-30 at 09:52 +0100, Ron Korving wrote: > > I'm a big fan of coalesce($param1, $param2, ..., $paramN) (or
firstset()).
> > The syntax allows for more than what ifsetor($var, $value) would do. > > Ifsetor() could be done in userspace, but I don't see how coalesce()
could,

Robert Cummings

20 years ago
On Sun, 2005-10-30 at 15:57, Ron Korving wrote:
> The function I used in my post does not generate an E_NOTICE, because a > call-by-reference can be done with an unset variable (that can be set from > the function). > > <?php > error_reporting(E_ALL); > > function ifsetor(&$var, $value) > { > return (isset($var)) ? $var : $value; > } > > echo ifsetor($bla, 'test'); > ?> > > This simply prints 'test' and shows no errors. In fact, a var_dump() on $bla > will show that it's become NULL. Never mind this whole ifsetor() in > userspace issue, because it's not applicable anyway, but just for the > record, this function does not generate an E_NOTICE.
Yes but it does generate a null assigned $bla variable in the calling scope... side effects, side effects, never know when they'll bite you in the rear. Cheers, Rob.
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'