Edit report at https://bugs.php.net/bug.php?id=65296&edit=1
ID: 65296
Comment by: work at nowhere dot de
Reported by: llmll at gmx dot de
Summary: Support named parameters in constructors to inline
initialize objects with new()
Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Hi, would really like to see this feature.
@ab, you did not understand the main request. Arrays *are* possible, but THESE
are the bad style you talk about. Initializing properties by strings is error
prone, since it is a concept brake: mapping anything-strings to
defined-constants.
The OP talked about named constants in constructors. What is the problem with
such a language feature?
Previous Comments:
------------------------------------------------------------------------
[2013-08-14 11:02:05] none at none dot de
I think the main point was missed: Original request was about Named Parameters.
(http://en.wikipedia.org/wiki/Named_parameter)
This will not extend the language or its features but enhance code readability
for both humans and IDEs.
------------------------------------------------------------------------
[2013-07-22 10:32:31] [email protected]
What i mean, each property should be contained in the class declaration before
it's actually defined in construct :)
------------------------------------------------------------------------
[2013-07-22 10:27:57] [email protected]
I really hope we won't turn php into javascript :) Please take a look, the
sugar you
talk about is possible with the following
class Props {
function __construct(array $argv) {
foreach($argv as $arg => $val) {
$this->$arg = $val;
}
}
}
$u = new Props(['hello' => 'world',
'good' => 'bye',
'obj' => new Props(['my' => 'god'])]);
Or even use func_get_args() to make $argv optional.
However this is a VERY bad style. Every object property should be defined with
the
class.
------------------------------------------------------------------------
[2013-07-19 21:11:24] llmll at gmx dot de
Just forgot to mention one more readability advantage:
It would support "nested" initializations, like it is now possible with arrays
full of strings
$object = new Object({
Property: $aValue,
Property2: $aValue2,
Subobject: new Object({
...
})
});
------------------------------------------------------------------------
[2013-07-19 21:05:31] llmll at gmx dot de
Of course its working, this is not about a bug but about sleekness and
PHP-to-programmer support ;-)
These assignment-lists are really lengthy and contain too many repetitions.
Its more like requesting the ternary operator instead of the "old" if ... then
... else ...
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=65296
--
Edit this bug report at https://bugs.php.net/bug.php?id=65296&edit=1