Re: [Concept] Extension methods
| From: | Rowan Tommins | Date: | Thu, 11 Aug 2022 15:10:28 +0000 |
| Subject: | Re: [Concept] Extension methods | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118414@lists.php.net to get a copy of this message | ||
On 11/08/2022 10:03, Alex Wells wrote:
You could argue the problem is that all of these are single-liners, so here are the same examples, but multiline formatted:When people talk about avoiding one-liners, they're not just talking about whitespace, but other refactoring. For example, introducing intermediate variables: $fileNameComponents = explode('_', $file->getName() ); $slicedComponents = array_slice($fileNameComponents, 4); // (most likely there's a better variable name, which would explain *why* we're ignoring those components) $className = Str::studly( implode('_', $slicedComponents) ); Or adding additional helper functions: $fileNameComponents = explode('_', $file->getName() ); $slicedComponents = array_slice($fileNameComponents, 4); $className = Str::studlyFromArray( $slicedComponents ); Or even, since "Str::studly" probably calls explode() internally anyway: $className = Str::studlyWithSlice( $file->getName(), 4 );
On 11 Aug 2022, at 07:36, Paul Crovella <paul.crovella@gmail.com> wrote: An extension method would shift a single argument from inside parentheses to the left of the function name. It just moves it. I don't see any impact here.
It doesn’t move it, it removes it. You’re technically not passing that value as an argument anymore anywhere in your code, your value becomes the implicit
expression you’re working with. That becomes even more obvious when multiple method calls are chained and don’t need to pass it into every function call.I think what Paul means is that "$foo->bar($baz)" and "bar($foo, $baz)" have the same "words" - the "$foo" is there in both cases, just in different positions. The same is true of nesting vs chaining: "$foo->bar()->baz()" has the same "words" as "baz( bar( $foo ) )", just in a different order. I do agree that the left-to-right order is nicer to read than the nested version, but that's largely opinion - it's not actually any shorter. Regards, -- Rowan Tommins [IMSoP]