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

donquixote created an issue. See original summary.

donquixote’s picture

I 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.

drunken monkey’s picture

I 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).

  • The usual argument of "would be a huge patch to change this now in Core".
  • I find this easy to read and also much easier to remember – just use the singular, done. Otherwise, for each such method, you'd need to remember what word goes in front.
  • I personally don't like it when method names don't start with a verb (especially if they contain several words). It doesn't appear to be contained in our coding standards, but I'd say it's a general standard in OOP everywhere.
joachim’s picture

I 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.