[PHP-DEV] Skipping of defaulted parameters.
Hello. Take the following simple code. ?php function foo($var1, $var2 = 2, $var3 = 3) { echo $var1, $var2, $var3\n; } foo(10); // 10, 2, 3 foo(10, 20); // 10, 20, 3 foo(10, 20, 30); // 10, 20, 30 foo(10, null, 30); // 10, , 30 foo(10,, 30); // Parse error. ? According to the manual A variable is considered to be null if it has not been set to any value yet [1]. By default, function arguments are passed by value [2]. When using default arguments, any defaults should be on the right side of any non-default arguments. [3] From this, the question I have is what do I pass when I want to use the default, WITHOUT having to supply the default value itself. On the surface, null should work. Null (according to [1]) is specifically saying that there is no value. But it is being interpreted as a value. I'm guessing the reason for null being interpreted as a value is really that the supplied argument count is used and any unsupplied arguments are defaulted. But null isn't a value ... that seems to be REALLY important to me. But from userland it is a value. Just a really odd one. I would argue that by having a null in the arguments, the intent is to NOT supply a value and have the default value used in the function. Possible solutions. 1 - Do nothing in core and implement is_null() checking to reinstate the default value. function foo($var1, $var2 = 2, $var3 = 3) { $var2 = is_null($var2) ? 2 : $var2; $var3 = is_null($var3) ? 3 : $var3; echo $var1, $var2, $var3\n; } 2 - Allow null to be supplied and have the default value be used for the argument. foo(10, null, 30); // would output 10, 2, 30 3 - New keyword of default or void to specifically indicate the intent to use the default value for the argument. foo(10, default, 30); // would output 10, 2, 30 4 - Allow missing arguments to default. foo(10,, 30); // Parse error. Option 4 would probably be the worse one to go for. Looking any number of languages that support defaults and you will see code like ... someFunction(param1,param7param11) sort of thing. Option 3, whilst does clearly indicate the intent, does introduce a new keyword. As a non core dev, I'm guessing we end up with naming conflicts. And something called default is probably going to be a big one. Void also. But using something like _ (yep, underscore), could be a solution here [4]. Option 2, after probably having to reject option 3, would be my choice. I want null to REALLY mean nothing. Just like it would be in foo(10). Option 1 is what I have to do at the moment. Regards, Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY [1] http://docs.php.net/manual/en/language.types.null.php [2] http://docs.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference [3] http://docs.php.net/manual/en/functions.arguments.php#functions.arguments.default [4] http://www.seoegghead.com/software/php-parameter-skipping-and-named-parameters.seo -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Continuing the build process after a failed build of an extension.
On 19 October 2010 14:51, Richard Quadling rquadl...@gmail.com wrote: On 19 October 2010 14:11, Derick Rethans der...@php.net wrote: --enable-snapshot-build Close. Will adapt. I don't want everything on, just the ignore build failures part. Thank you. Or I can just use ... nmake /I /I Ignore exit codes from commands Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Skipping of defaulted parameters.
On Wed, October 20, 2010 6:58 am, Richard Quadling wrote: foo(10,, 30); // Parse error. I thought this used to work... I would argue that by having a null in the arguments, the intent is to NOT supply a value and have the default value used in the function. Unfortunately, no. There are times when I want to over-ride the default, and shove NULL in as the value... 1 - Do nothing in core and implement is_null() checking to reinstate the default value. I believe that this is probably Best Practice in current PHP. Validate your incoming parameters consistent with the intent of the code-base in question. 2 - Allow null to be supplied and have the default value be used for the argument. foo(10, null, 30); // would output 10, 2, 30 -1 3 - New keyword of default or void to specifically indicate the intent to use the default value for the argument. foo(10, default, 30); // would output 10, 2, 30 This seems reasonable to me. 4 - Allow missing arguments to default. foo(10,, 30); // Parse error. Also reasonable. I'd even say both 34 together are in keeping with PHP spirit, to allow the lazy scripters to use 10,,30 and the formal developers to use DEFAULT. Option 4 would probably be the worse one to go for. Looking any number of languages that support defaults and you will see code like ... someFunction(param1,param7param11) It does get ugly fast for large numbers of arguments... But any function with more than a handful of arguments is already asking for trouble... At that point you should be passing in a data structure / instance / array or something other than so many parameters. Perhaps a constant like PHP_DEFAULT rather than a new keyword? But using something like _ (yep, underscore), could be a solution here [4]. Icky. :-) Option 2, after probably having to reject option 3, would be my choice. I want null to REALLY mean nothing. Just like it would be in foo(10). Alas, NULL behaves more like an actual value sometimes, depending on which functions you use... isset versus array_key_exists, for example. -- brain cancer update: http://richardlynch.blogspot.com/search/label/brain%20tumor Donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=FS9NLTNEEKWBE -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Skipping of defaulted parameters.
Hi! 3 - New keyword of default or void to specifically indicate the intent to use the default value for the argument. Actually, 'default' is already a keyword (switch!), while _ is an actual function name (gettext). So default, syntactically, can work, while _ can't. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DEV] Skipping of defaulted parameters.
Yup. I'm leaning towards Richard's idea of a predefined constant rather than a new keyword which would effectively do the same thing. The only issue I can see coming out of using the constant would be it's represented value. If a compile-time keyword were used, at least it could be caught there without running into any value clashing issues. -Original Message- From: Stas Malyshev [mailto:smalys...@sugarcrm.com] Sent: Wednesday, October 20, 2010 1:37 PM To: rquadl...@googlemail.com Cc: Richard Quadling; PHP internals Subject: Re: [PHP-DEV] Skipping of defaulted parameters. Hi! 3 - New keyword of default or void to specifically indicate the intent to use the default value for the argument. Actually, 'default' is already a keyword (switch!), while _ is an actual function name (gettext). So default, syntactically, can work, while _ can't. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DEV] PHP 5.4 alpha release date?
Hi! New PLAN for 5.4 Alpha or Beta? Derick mailto:derick%40php.net ? pAUL gAO / gaochun...@gmail.com