Some classes have both regular getters, and "parameterized" getters.
$items = $list->getItems(); // All items.
$item = $list->getItem($key); // Only the one item matching the key.
The "parameterized" getters are not really getters in the traditional sense.
I would like to propose a naming convention that makes them easier to distinguish from regular getters, and hopefully make code easier to read.
Maybe others have alternative ideas.
$items = $list->getItems(); // All items.
$item = $list->keyGetItem($key); // Only the one item matching the key.
Use case: ExtensionList
A possible use case would be the issue about ExtensionList, #2208429: Extension System, Part III: ExtensionList, ModuleExtensionList and ProfileExtensionList
$module_list->getExtensions();
$module_list->getExtension($extension_name); // Current proposal in e.g. #2208429-165
$module_list->nameGetExtension($extension_name); // New, option 1
$module_list->extensionGetExtension($extension_name); // New, option 2, which I find awkward.
In this example, besides distinguishing classical getters from parameterized getters, we also need to distinguish extension machine name, extension human name, extension object, and extension info array. So imo the ->nameGet*() would be better than ->extensionGet*().
This would also work for parameterized setters:
$extension_list->setFilename($extension_name, $filename) { // Current proposal in e.g. #2208429-165
$extension_list->nameSetFilename($extension_name, $filename); // New, option 1
Since we are not currently doing this anywhere I am aware of, this needs a coding standards discussion before it can be applied.
Comments
Comment #2
donquixote commentedI made this a habit for code I write myself (contrib modules, libraries, client projects), and I find it to work fine. It reduces the second-guessing about method behavior.
This may not be enough to convince anyone, I am just mentioning it anyway.
Comment #3
drunken monkeyI don't think this even needs its own standard, it seems to me to be a bit too specialized for that. We can't have a standard for every single programming scenario. (It's a close call in this case, though, so I'm not completely against it.)
If we want one, I'm +1 for keeping the current standard of
getItem($key).Comment #4
quietone commentedComment #5
joachim commentedI think this is maybe going to be obsolete soon, as PHP8's property types and readonly properties mean that getters and setters won't be necessary most of the time.