Instead of using function_exists() to verify if a callback exists, Drupal could use is_callable(); this would allow to use object and class methods as callbacks.

I am setting the release to Drupal 8, as I think it's too late to make such changes in Drupal 7.

Comments

avpaderno’s picture

Title: Use is_callable() to verify if a callback exists » Allow to use object and class methods as callbacks

I am changing the issue title; probably Drupal core code needs to be changed more to allow to use methods as callbacks.

xano’s picture

Title: [Policy, no patch] Deprecate function_exists() in favor of is_callable() » Deprecate function_exists() in favor of is_callable()
Status: Needs review » Active

I propose we fully deprecate the use of function_exists() in core and replace all occurences with is_callable(), except, of course, when checking for a specific predefined function and not for a callback.

API changes

No BC breaks.

Related issues

xano’s picture

Title: Allow to use object and class methods as callbacks » [Policy, no patch] Deprecate function_exists() in favor of is_callable()
Priority: Normal » Major
Status: Active » Needs review

Settings to needs review, so we can get reviews on this idea.

sidharthap’s picture

Title: Deprecate function_exists() in favor of is_callable() » [Policy, no patch] Deprecate function_exists() in favor of is_callable()
Status: Active » Needs review

@Xano there are some other modules also contains function_exists and not listed here. like system module. Is it to remove all existence of function_exists from Drupal core ?

xano’s picture

If there are any other files or modules that still use function_exists() even though they shouldn't (note that some usages are valid), please create issues for them and add links to them in the issue summary.

xjm’s picture

Category: Feature request » Task
Issue summary: View changes

 

dawehner’s picture

For sure, it is not a bad idea to support any kind of callable.

In general there are usecases where function_exists() is more correct and provides a bit more local semantic:

views/views.module
194:      if (!function_exists('theme_' . $def['theme'])) {
dave reid’s picture

Issue tags: +callables
mile23’s picture

Title: [Policy, no patch] Deprecate function_exists() in favor of is_callable() » [Policy, no patch] Normalize on usage of is_callable() instead of function_exists()

I see 137 occurrences if function_exists(). Some are within inline comments, though.

Do we want to move on this? Split it up into separate issues?

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Since the last comment, there are now 145 occurrences of function_exists(). I think this would still be worth tackling.

avpaderno’s picture

Version: 8.6.x-dev » 8.8.x-dev

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Category: Task » Plan

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

borisson_’s picture

I agree, this is a good thing to do. Can we use phpstan to only allow the is_callable function?

quietone’s picture

Priority: Major » Normal
Status: Needs review » Reviewed & tested by the community

There is agreement here on making this change. And, in fact, there have been several issues implementing the change.

A policy decision should be accompanied with documentation. And there has been a suggestion to enforce this change. That implies this could be a coding standard. Although forbidding the use of a PHP function seems too string.

I have created a separate issue for doing the actual conversion, #3520416: [meta] Handle calls to functions that still require .module file autoloading.. Anyone wanting to discuss making this a coding standard should open an issue in the Coding Standards project.

With all that I think the work on this part is complete.

catch’s picture

Status: Reviewed & tested by the community » Fixed

With #3520416: [meta] Handle calls to functions that still require .module file autoloading. open I think we can just close this as fixed. I don't really see adding this as a coding standard, there are cases where you know you're looking for a specific function, like specific PHP functions that may or may not exist depending on installation.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

quietone’s picture

Version: 11.x-dev » 11.2.x-dev

Changing to latest version when this was closed.