Req #44298 [Opn->Fbk]: Allow switch() to track multiple variables.
| From: | danack@php.net | Date: | Wed, 07 Jan 2015 20:51:23 +0000 |
| Subject: | Req #44298 [Opn->Fbk]: Allow switch() to track multiple variables. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189715@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=44298&edit=1
ID: 44298
Updated by: danack@php.net
Reported by: php at dafydd dot com
Summary: Allow switch() to track multiple variables.
-Status: Open
+Status: Feedback
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Mac/Doesn't matter
PHP Version: 5.2.5
Block user comment: N
Private report: N
New Comment:
Hi, this is actually possible already in PHP already, and doesn't need any extra functionality.
You can specify a function to be called to combine multiple arguments into a single value inside
case statements like this:
function f($value, $access = null) {
$knownAccessTypes = [
'read-only',
'read-write'
];
if (in_array($access, $knownAccessTypes)) {
return $value.$access;
}
return $value;
}
function multiSwitchFunction($value, $access = null) {
switch (f($value, $access)) {
case f('a', 'read-only'): return 'a r';
case f('a', 'read-write'): return 'a w';
case f('a'): return 'a default';
case f('b', 'read-only'): return 'b r';
case f('b', 'read-write'): return 'b w ';
case f('c', 'read-only'): return 'c r';
case f('c', 'read-write'): return 'c w';
case f('d', 'read-only'): return 'd r';
case f('d', 'read-write'): return 'd w' ;
default: return "unknown";
}
}
var_dump(multiSwitchFunction('a', 'read-write'));
var_dump(multiSwitchFunction('a', 'foobar'));
var_dump(multiSwitchFunction('d', 'read-write'));
var_dump(multiSwitchFunction('d', 'foobar'));
If that doesn't suite your needs, it might be better to discuss the exact feature you would
like on the PHP internals list, where an RFC could be discussed.
Previous Comments:
------------------------------------------------------------------------
[2008-02-29 23:41:29] php at dafydd dot com
Description:
------------
In writing with PHP, I have frequently come across situations where I would have to nest switch()
functions. So, I thought of an alternative.
Reproduce code:
---------------
Current code:
Suppose I have one variable limited to four specific values, and a second variable that identifies
whether or not the first variable can be changed. Right now, I have to do this:
switch ($value) {
case 'a':
switch ($access) {
case 'read-only':
case 'read-write':
}
case 'b':
switch ($access) {
case 'read-only':
case 'read-write':
}
case 'c':
switch ($access) {
case 'read-only':
case 'read-write':
}
case 'd':
switch ($access) {
case 'read-only':
case 'read-write':
}
}
Expected result:
----------------
Instead of doing that, how about changing switch to track multiple variables. Perhaps like this:
switch ($value, $access) {
case ('a', 'read-only'):
case ('a', 'read-write'):
case ('a', default):
case ('b', 'read-only'):
case ('b', 'read-write'):
case ('b', default):
case ('c', 'read-only'):
case ('c', 'read-write'):
case ('c', default):
case ('d', 'read-only'):
case ('d', 'read-write'):
case ('d', default):
case (default, default):
}
This would reduce repetition in coding!
Actual result:
--------------
I fully anticipate that this would be an "incorporate into PHP 7" class of new feature.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=44298&edit=1