Open wernerkrauss opened 7 years ago
method_exists($item, 'hasMethod')
? 😄
@robbieaverill sure... but hasMethod()
also checks methods on DataExtensions, so it's not just a replacement.
I wrote a local solution:
protected function getKeyOfItem($item, $index) {
if (is_object($item)) {
if (method_exists($item, 'hasMethod') && $item->hasMethod($index)) {
return $item->$index();
}
//fallback for plain PHP objects
if (method_exists($item, $index)) {
return $item->$index();
}
return $item->$index;
}
return $item[$index];
}
If you think it's a good enhancement I can make a PR against 3.5
Seems like a reasonable patch.
@wernerkrauss did you ever submit the patch to 3.5?
@wernerkrauss It'd be better to group the condition if you can help it, rather than duplicate the action. Do you want to PR? :P
https://github.com/silverstripe/silverstripe-framework/blob/3.5/model/GroupedList.php#L21