5 ms·
> break the function signature into one-line-per-parameter, which is vile Speak for yourself. If each line indicates what it is the parameter for I couldn’t ca
by Aeolun 6y ago
> break the function signature into one-line-per-parameter, which is vile
Speak for yourself. If each line indicates what it is the parameter for I couldn’t care less.
Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.
- arcosdev 6y agoWhy not use a dictionary or parameter object when you get past 2 arguments? Or is that not a thing in PHP?
- flemhans 6y agoThat's what the above comment was referring to when saying people to things like: call_user_func_array($function, ['length' => $n, 'foo' => $bar, ...]);
- stareatgoats 6y ago> and basically nobody does that anyway. That's it, I'm officially a nobody :-)
- sildur 6y ago> leaves it up to the called function to sanity-check the input types instead of letting the compiler do it, and basically nobody does that anyway. You don’t check the input types?
- stareatgoats 6y agoYou're reading the sentence incorrectly: "and basically nobody does that anyway", means the preceding type checking argument you refer to is moot. So I insist, I am a nobody (according to this definition). Btw I don't always sanity check the parameters, mostly at the system edges only.
- z92 6y agoNot in get-it-done, hackish scripts.
- webignition 6y agoI'd think that such a solution doesn't solve but merely moves the problem. A method with N arguments, where N > 2, with a call that is messy due to the number of arguments could instead take a single argument being a parameter object where the constructor for the parameter object takes the same N arguments. A constructor with N arguments with a call that is messy due to the number of arguments ...
- SAI_Peregrinus 6y agoAah, so then you use the Builder Pattern! Instead of one constructor taking N arguments, have a constructor that takes no arguments and N function calls to initialize the constructed values. It's cleaner because it's a named pattern! /s
- beberlei 6y agoThat is possible, but requires writing a class, which increases mental load
- raziel2p 6y agoWith a dictionary (array in PHP) you lose strict typing and it's possible for the person constructing it to make a typo and it won't cause an error, the called function will just think an optional argument was left out. Writing and maintaining parameter classes is just tedious.
- wonderlg 6y agoTypescript can do it, so why not add the same feature to PHP as well? As a JS/TS developer, all I see is avoiding two characters: {}, everything else is possible in JavaScript and well-typed in TypeScript. PHP 8 save( value: 3 ) JS 1 save({ value: 3 })
- duckerude 6y agoPHP's strict typing is only enforced at runtime, and because of that, its type system is very simple, unlike Typescript's. You can enforce that a variable is an array, but not that it's an array of ints, let alone that it's an array with certain types under certain keys. Although there is external tooling that uses static analysis for such more complicated types.
- xellisx 6y agoWe really need structs in PHP. I drew up a draft, but didn't get very far with it. https://github.com/ellisgl/PHP-RFC-Struct-Data-Type https://github.com/ellisgl/PHP-RFC-Struct-Data-Type
- gvjddbnvdrbv 6y agoI've tried it but it gets messy.
- Lammy 6y agoYou can get the best of both approaches in PHP 5.6 or later using the argument unpacking "..." operator that works similarly to the */"splat" operator in Python or Ruby: https://www.php.net/manual/en/migration56.new-features.php https://www.php.net/manual/en/migration56.new-features.php
- gremlinsinc 6y agoit's not the best of both worlds though, splat should only be used on something where you're basically doing the same thing to a bunch of things. like add(1,2,3,4,5) -> function add(...$nums){ $count = 0; foreach($nums as $num) { $count += $num; } } If you know what arguments you're getting, then the more sound way would be just use an associative array, but either way you really don't get great type checking. This is a much better solution.
- gremlinsinc 6y agoI do this all the time, but it feels wrong... and you don't get type checking, this feels like it makes more sense and logical.
- waheoo 6y agoOther than plain object constructs these kinds of functions are unheard of and better handled with a parameter object or array. I can't help but feel if they just set up a clean way to name construct Params for classes this wouldn't need to happen.
- raziel2p 6y agoWhy are they better handled with a parameter object or array? https://news.ycombinator.com/item?id=23962955 https://news.ycombinator.com/item?id=23962955 What is the functional difference between a named construct "Params" and just allowing named parameters?
- deleted 6y ago[deleted]
- chriswarbo 6y ago> What is the functional difference between a named construct "Params" and just allowing named parameters? That "just" is doing a lot of work. I've not used PHP for a few years, but off the top of my head I can forsee the following problems: - `call_user_func` would need to change to support this, e.g. accepting and passing-along named parameters. - `call_user_func_array` would need to change, e.g. passing along associative-array arguments as named parameters. - `func_get_args` would need to change, e.g. returning an associative array Other parts of the standard library, like ReflectionParameter, would also need corresponding changes. On their own I don't think those are necessarily bad. However, this would break a lot of code which relies on those functions and their invariants. For example, currying and partial application would become more complicated (e.g. http://chriswarbo.net/blog/2014-02-21-partial_application___currying_in_php.html http://chriswarbo.net/blog/2014-02-21-partial_application___... ). I'm not necessarily against this change, but I would make the following points: Extending APIs is a breaking change, since it breaks invariants/contracts that previously held (in this case by function definitions and call sites). This breaks backwards-compatibility, and that should be acknowledged and stated explicitly, rather than ignored. Here's a quick example of code which breaks, since it relies on the now-violated invariant that the parameter order of a function call matches that of the definition: function call_with_strings() { $args = func_get_args(); $f = array_shift($args); $strings = []; foreach ($args as $arg) { $strings[] = strval($arg); } return call_user_func($f, $strings); } Secondly: there's a definite bandwagon effect between scripting languages like PHP/Python/JS/etc., for better (easier to transfer between) or worse (uncompelling monocultures lacking innovation/USP). Just because a feature exists in one, doesn't necessarily make it a good idea for the others (or the first, for that matter!); conversely, there are lots of great features out there which scripting languages don't seem to have picked up, and which are hardly ever brought up in these discussions. For example: - Default arguments are generally supported, e.g. `parseInt(i, base=10)`. Alternative approaches to the same problem, like currying, are mostly absent, e.g. `parseIntFor(base, i); parseInt = parseIntFor(10);` - Generators are generally supported, e.g. `for x in foo: yield x`. Alternative approaches to the same problem, like delimited continuations, are mostly absent, e.g. `reset; for x in foo: shift(x);`. This is especially interesting, since it's just an extension of exception handling, which is another generally-supported feature! - Named parameters are generally supported, e.g. `foo(a=1, c=5)`. Alternative approaches to the same problem, like row-polymorphism, are mostly absent, e.g. `foo(['a' => 1, 'c' => 5])`.
- masklinn 6y ago> Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy. It can be super useful even for functions with low arity e.g. `max(seq, key=func)`.
- dariusj18 6y agoI'll use it for functions with two parameters, array functions and the order you pass in arguments is not standardized. I can start just using named functions to make it clear and easier to read/write.