# [RFC] skipping optional parameters

- Thread: https://staging.externals.io/message/60126
- Original archive: https://news-web.php.net/php.internals/60126
- Started by Stas Malyshev on 2012-04-17T22:54:41+00:00
- Messages: 63
- RFC: Skipping optional parameters (Declined) — https://staging.externals.io/rfc/skipparams.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://staging.externals.io/message/60126.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://staging.externals.io/search.md?q={query} · RFCs at https://staging.externals.io/rfc.md · JSON API at https://staging.externals.io/api · documentation at https://staging.externals.io/llms.txt

---

## #60126 — Stas Malyshev — 2012-04-17T22:54:41+00:00

URL: https://staging.externals.io/message/60126#60126

Hi!

One of the annoying things I've encountered in working with PHP was
dealing with functions having long optional parameter lists, especially
if you need to change only the last one - you have to copy all the
defaults. Full named params implementation would solve it, probably, but
before we have that here's an easier solution for part of the problem:

https://wiki.php.net/rfc/skipparams

Basically, it allows you to do this:

create_query("deleted=0", "name",,, /*report_errors*/ true);

Implementation of it is here:
https://github.com/smalyshev/php-src/tree/skip_params
All things described in RFC seem to be working, please tell me if I
forgot anything. If it's accepted I'll also add tests, etc. of course.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60129 — Ryan McCue — 2012-04-17T23:17:00+00:00

URL: https://staging.externals.io/message/60126#60129 · In reply to: #60126

Stas Malyshev wrote:
> Basically, it allows you to do this:
>
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>

I'm a huge fan of this idea, for all the reasons listed on the RFC and 
one additional one: it brings normal functions in line with the syntax 
for list() - We can skip parameters in list() using the same syntax, so 
to my eye, this brings consistency.

(Of course, list() is a construct, not a function, but considering how 
similar the syntax is, I think this makes sense.)

---

## #60130 — Jordi Boggiano — 2012-04-17T23:23:17+00:00

URL: https://staging.externals.io/message/60126#60130 · In reply to: #60126

> One of the annoying things I've encountered in working with PHP was
> dealing with functions having long optional parameter lists, especially
> if you need to change only the last one - you have to copy all the
> defaults. Full named params implementation would solve it, probably, but
> before we have that here's an easier solution for part of the problem:
> 
> https://wiki.php.net/rfc/skipparams
> 
> Basically, it allows you to do this:
> 
> create_query("deleted=0", "name",,, /*report_errors*/ true);

Pretty cool especially since it avoids the problem when refactoring code
of knowing whether the use of the default param was intentional or just
as a filler to reach a later parameter.

I do have two remarks though:

- I hope it doesn't mean giving up on named args, because that would
look way cleaner.

- Would it be possible to allow trailing commas in combination to this?
i.e. foo("arg",) would just call foo with "arg" and then the default
value for the second arg if there is one, or just nothing. That would
make multi-line function calls a bit cleaner, and more similar to arrays.

Cheers

-- 
Jordi Boggiano
@seldaek - http://nelm.io/jordi

---

## #60131 — Yasuo Ohgaki — 2012-04-17T23:24:35+00:00

URL: https://staging.externals.io/message/60126#60131 · In reply to: #60126

Hi,

2012/4/18 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> One of the annoying things I've encountered in working with PHP was
> dealing with functions having long optional parameter lists, especially
> if you need to change only the last one - you have to copy all the
> defaults. Full named params implementation would solve it, probably, but
> before we have that here's an easier solution for part of the problem:
>
> https://wiki.php.net/rfc/skipparams
>
> Basically, it allows you to do this:
>
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>
> Implementation of it is here:
> https://github.com/smalyshev/php-src/tree/skip_params
> All things described in RFC seem to be working, please tell me if I
> forgot anything. If it's accepted I'll also add tests, etc. of course.

Introducing "undefined" might be good.
Intention becomes clearer and we may do

if ($var === undefined) {
  echo '$var is undefined';
}

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #60132 — Stas Malyshev — 2012-04-17T23:26:44+00:00

URL: https://staging.externals.io/message/60126#60132 · In reply to: #60131

Hi!

> Introducing "undefined" might be good.
> Intention becomes clearer and we may do
> 
> if ($var === undefined) {
>   echo '$var is undefined';
> }

What is undefined and how $var gets assigned it?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60134 — Yasuo Ohgaki — 2012-04-17T23:33:33+00:00

URL: https://staging.externals.io/message/60126#60134 · In reply to: #60132

Hi,

2012/4/18 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> What is undefined and how $var gets assigned it?

I'm thinking undefined like JavaScript.

AFAIK, engine does not have undefined state.
It may be too many changes required.

Just skipping would work and I'm not against it.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #60135 — Yasuo Ohgaki — 2012-04-17T23:38:39+00:00

URL: https://staging.externals.io/message/60126#60135 · In reply to: #60134

Hi,

It's undefined, so it does not have to keep track state ;)
But it may require a lot of changes, so I don't matter much.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net



2012/4/18 Yasuo Ohgaki <yohgaki@ohgaki.net>:
> Hi,
>
> 2012/4/18 Stas Malyshev <smalyshev@sugarcrm.com>:
> > […]
>
> I'm thinking undefined like JavaScript.
>
> AFAIK, engine does not have undefined state.
> It may be too many changes required.
>
> Just skipping would work and I'm not against it.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #60136 — Simon J Welsh — 2012-04-17T23:44:39+00:00

URL: https://staging.externals.io/message/60126#60136 · In reply to: #60126

> Hi!
> 
> One of the annoying things I've encountered in working with PHP was
> dealing with functions having long optional parameter lists, especially
> if you need to change only the last one - you have to copy all the
> defaults. Full named params implementation would solve it, probably, but
> before we have that here's an easier solution for part of the problem:
> 
> https://wiki.php.net/rfc/skipparams
> 
> Basically, it allows you to do this:
> 
> create_query("deleted=0", "name",,, /*report_errors*/ true);
> 
> Implementation of it is here:
> https://github.com/smalyshev/php-src/tree/skip_params
> All things described in RFC seem to be working, please tell me if I
> forgot anything. If it's accepted I'll also add tests, etc. of course.


Hi

I like this proposal. One thing pops out at me though.

Since `func_num_args()` won't be able to be used to see if an argument is set, could a func_isset_arg() be added? So something like:
if(func_num_args() > 2) $value = func_get_arg(2);
would become:
if(func_isset_arg(2)) $value = func_get_arg(2);
---
Simon Welsh

---

## #60138 — Yasuo Ohgaki — 2012-04-17T23:55:17+00:00

URL: https://staging.externals.io/message/60126#60138 · In reply to: #60136

Hi,

2012/4/18 Simon J Welsh <simon@welsh.co.nz>:
> 
>
> > […]
>
>
> Hi
>
> I like this proposal. One thing pops out at me though.
>
> Since `func_num_args()` won't be able to be used to see if an argument is set, could a func_isset_arg() be added? So something like:
> if(func_num_args() > 2) $value = func_get_arg(2);
> would become:
> if(func_isset_arg(2)) $value = func_get_arg(2);

Good point.
For `func_get_arg()`, having "undefined" is mandatory,
otherwise we cannot tell if it is defined or not.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #60137 — Stas Malyshev — 2012-04-17T23:55:50+00:00

URL: https://staging.externals.io/message/60126#60137 · In reply to: #60134

Hi!

> I'm thinking undefined like JavaScript.

Well, in PHP null is closest, but a bit different. I.e. in JS undefined
a is an error, but a[0] where a is empty is "undefined". In PHP in both
cases you get null and notice, and in both cases you can use isset/empty
to check for it.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60139 — Stas Malyshev — 2012-04-17T23:56:55+00:00

URL: https://staging.externals.io/message/60126#60139 · In reply to: #60136

Hi!

> Since `func_num_args()` won't be able to be used to see if an argument is set, could a func_isset_arg() be added? So something like:
> if(func_num_args() > 2) $value = func_get_arg(2);
> would become:
> if(func_isset_arg(2)) $value = func_get_arg(2);

I think it's easier to just do `func_get_args()` and check for isset there
:) You usually don't have too many args so getting all of them is not a
big problem.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60140 — Etienne Kneuss — 2012-04-18T00:03:27+00:00

URL: https://staging.externals.io/message/60126#60140 · In reply to: #60139

Hi,



> Hi!
>
> > […]
>
> I think it's easier to just do `func_get_args()` and check for isset there
> :) You usually don't have too many args so getting all of them is not a
> big problem.
>

+1

BTW: the diffs show a lot of WS changes that you should probably fix.

Best,

> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> 
> 
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #60141 — Nikita Popov — 2012-04-18T00:08:01+00:00

URL: https://staging.externals.io/message/60126#60141 · In reply to: #60140

> Hi,
>
>
> 
> > […]
>
> +1
>
> BTW: the diffs show a lot of WS changes that you should probably fix.
On a side note, one can pass ?w=1 to the github diff, so it will show
up without WS changes:
https://github.com/smalyshev/php-src/commit/0f78493d3e96a627caa8258ee9e618301b6b3511?w=1
Makes is a bit easier to spot the relevant parts.

I'm also +1 on the proposal :)

Nikita

---

## #60142 — Stas Malyshev — 2012-04-18T00:10:20+00:00

URL: https://staging.externals.io/message/60126#60142 · In reply to: #60141

Hi!

> > […]

That's Eclipse, it loves to strip trailing whitespace... Which btw shows
for some reason we have lots of it in our code.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60143 — Yasuo Ohgaki — 2012-04-18T00:19:51+00:00

URL: https://staging.externals.io/message/60126#60143 · In reply to: #60139

Hi,

2012/4/18 Stas Malyshev <smalyshev@sugarcrm.com>:
> Hi!
>
> > […]
>
> I think it's easier to just do `func_get_args()` and check for isset there
> :) You usually don't have too many args so getting all of them is not a
> big problem.

This works.
I think it's fine with this.

There may be a few codes that have BC issues, but
it's new PHP anyway.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #60145 — Alan Knowles — 2012-04-18T01:11:08+00:00

URL: https://staging.externals.io/message/60126#60145 · In reply to: #60126

> Hi!
>
> https://wiki.php.net/rfc/skipparams
>
> Basically, it allows you to do this:
>
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>
> Implementation of it is here:
> https://github.com/smalyshev/php-src/tree/skip_params
> All things described in RFC seem to be working, please tell me if I
> forgot anything. If it's accepted I'll also add tests, etc. of course.
>
Hate to be contrary, it does raise a few questions.

a) it kind of encourages using long lists of arguments (which is not 
normally regarded as good practice)

b) the usual recommendation for solving that issue is to accept an array 
(fake named arguments)

c) the current situation would syntax error out on ,, as a function call 
argument, which is handy if you accidentally typed ,, twice by accident. 
if this changes, not only would you loose this syntax error, but it 
would do unpredictable things.

For my 2c, does not look like a very good idea..

Regards
Alan

---

## #60146 — Stas Malyshev — 2012-04-18T01:18:20+00:00

URL: https://staging.externals.io/message/60126#60146 · In reply to: #60145

Hi!

> a) it kind of encourages using long lists of arguments (which is not 
> normally regarded as good practice)

This ship has sailed long ago, these lists are the reality. And
rewriting all the code to change all function calls is in most cases
completely infeasible.

> c) the current situation would syntax error out on ,, as a function call 
> argument, which is handy if you accidentally typed ,, twice by accident. 
> if this changes, not only would you loose this syntax error, but it 
> would do unpredictable things.

There are many situations where you could type something by accident and
have it change the meaning of the call. For example, if to type:
if($foo); bar(); then bar() would execute always. This does not prevent
us from using ; as statement separator or from using if's.


-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60149 — Adam Jon Richardson — 2012-04-18T03:48:08+00:00

URL: https://staging.externals.io/message/60126#60149 · In reply to: #60126

On Tue, Apr 17, 2012 at 6:54 PM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> Hi!
>
> One of the annoying things I've encountered in working with PHP was
> dealing with functions having long optional parameter lists, especially
> if you need to change only the last one - you have to copy all the
> defaults. Full named params implementation would solve it, probably, but
> before we have that here's an easier solution for part of the problem:
>
> https://wiki.php.net/rfc/skipparams
>
> Basically, it allows you to do this:
>
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>
> Implementation of it is here:
> https://github.com/smalyshev/php-src/tree/skip_params
> All things described in RFC seem to be working, please tell me if I
> forgot anything. If it's accepted I'll also add tests, etc. of course.


Stas, this is fantastic! Not a voter, but I would be very thankful for this
addition to PHP.

Adam

---

## #60153 — Alexey Shein — 2012-04-18T05:56:28+00:00

URL: https://staging.externals.io/message/60126#60153 · In reply to: #60126

Hi!

My opinion is that solution tries to overcome bad consequences of
legacy code, when it's not feasible to change something without
breakage a lot of code, although the real solution is to refactor
functions with long variable lists (as Uncle Bob says), maybe this
should be noted in documentation of this feature.

Alan nicely highlighted all cons of this feature, but instead of
voting against it I suggest to put that into the docs, so people can
use it wisely and understand consequences.

One question about implementation:
Given we have this function
>   function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) {}
and this statement
> On the engine level, it will be implemented by putting `NULL` in the place where the parameter is passed.

what value $join_type will get on this call?
create_query("deleted=0", "name",,, /*report_errors*/ true);

 `NULL` or empty string? I.e. will optional parameters always get NULLs
or their default values?
Thanks.
-- 
Regards,
Shein Alexey

---

## #60155 — Peter Lind — 2012-04-18T06:01:40+00:00

URL: https://staging.externals.io/message/60126#60155 · In reply to: #60153

*snip*

>
> One question about implementation:
> Given we have this function
> > […]
> and this statement
> > […]
>
> what value $join_type will get on this call?
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>
> `NULL` or empty string? I.e. will optional parameters always get NULLs
> or their default values?

It needs to be the default values, otherwise it will be a huge WTF for
developers.

With that in mind: +1 if default values are used when skipping
parameters, -1 if `NULL` is used when skipping parameters (not that I
have voting power, just showing preferences/support).

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype

---

## #60156 — Stas Malyshev — 2012-04-18T06:40:52+00:00

URL: https://staging.externals.io/message/60126#60156 · In reply to: #60153

Hi!

> `NULL` or empty string? I.e. will optional parameters always get NULLs
> or their default values?

Their default values. That's the whole point of it - if I wanted just
nulls, I could put nulls in the call, but if I want the actual default,
I'd have to find the definition and copy-paste from there.


-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #60158 — Alexey Shein — 2012-04-18T06:46:37+00:00

URL: https://staging.externals.io/message/60126#60158 · In reply to: #60156

18 апреля 2012 г. 11:40 пользователь Stas Malyshev
<smalyshev@sugarcrm.com> написал:
> Hi!
>
> > […]
>
> Their default values. That's the whole point of it - if I wanted just
> nulls, I could put nulls in the call, but if I want the actual default,
> I'd have to find the definition and copy-paste from there.

Thank you, that's what I wanted to hear.

>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227



-- 
Regards,
Shein Alexey

---

## #60159 — Adam Harvey — 2012-04-18T06:57:31+00:00

URL: https://staging.externals.io/message/60126#60159 · In reply to: #60126

> create_query("deleted=0", "name",,, /*report_errors*/ true);

I like it. At the risk of bikeshedding, can we have a token rather
than just a series of commas — say reusing the default keyword so it
would look like this instead:

create_query("deleted=0", "name", default, default, /*report_errors*/ true);

That seems easier to read to my eyes than counting commas.

Thanks,

Adam

---

## #60162 — Ferenc Kovacs — 2012-04-18T07:18:47+00:00

URL: https://staging.externals.io/message/60126#60162 · In reply to: #60126

On Wed, Apr 18, 2012 at 12:54 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> Hi!
>
> One of the annoying things I've encountered in working with PHP was
> dealing with functions having long optional parameter lists, especially
> if you need to change only the last one - you have to copy all the
> defaults. Full named params implementation would solve it, probably, but
> before we have that here's an easier solution for part of the problem:
>
> https://wiki.php.net/rfc/skipparams
>
> Basically, it allows you to do this:
>
> create_query("deleted=0", "name",,, /*report_errors*/ true);
>
> Implementation of it is here:
> https://github.com/smalyshev/php-src/tree/skip_params
> All things described in RFC seem to be working, please tell me if I
> forgot anything. If it's accepted I'll also add tests, etc. of course.
>
>
I would prefer Named parameters, but this is still better than the current
situation.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #60163 — Yasuo Ohgaki — 2012-04-18T07:43:44+00:00

URL: https://staging.externals.io/message/60126#60163 · In reply to: #60162

Hi,

2012/4/18 Ferenc Kovacs <tyra3l@gmail.com>:
> I would prefer Named parameters, but this is still better than the current
> situation.

Something like

setcookie( name:'mycookie', value:'abc', secure:true, httponly:true );

is really nice to have. Source code will be much more readable.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

Next page: https://staging.externals.io/message/60126.md?page=2
