Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Issue title pretty much sums it up. Let's use the PHP function designed for detecting extensions, rather than checking to see if a function exists.
Alternatively, if there's a reason for not doing that, I'd love to hear it :)
Comment | File | Size | Author |
---|---|---|---|
#9 | 1942432_9-extension-loaded.patch | 18.46 KB | cweagans |
Comments
Comment #1
VSKor CreditAttribution: VSKor commentedHi!
Below few results of some manual testing.
Taken from http://www.merkushin.com/function_exists-vs-extension_loaded
I get little different results (i use real functions to check)
There is no big deal between those functions, but anyway according this testing "extension_loaded" work slower.
Check please.
Comment #2
VSKor CreditAttribution: VSKor commentedComment #3
pounardYou have 1,000,000 calls of each running with a difference of 0.05 second, which is an actual difference of 0,00000005 (10^-7) second for a single call, this is absolutely not significant (even with 1000 calls it's still not significant) and I bet that the 0.05 second is more an artefact of something else interfering with the actual run than an actual delta between both. Performance will not be a concern for this issue :)
Comment #4
tstoecklerNote that the existance of an extension is being checked just to be able to use functions that that extension provides it might actually be safer to use
function_exists()
because functions provided by an extension can be disabled with thedisable_functions
PHP ini setting, even if an extension is enabled. Thus,extension_loaded()
will returnTRUE
but the function call will still fatal in that case.function_exists()
will returnFALSE
.Doesn't mean we shouldn't do this, just wanted to bring this up, because this has bitten people before. I can't find the issue ATM, however.
Comment #5
pounarddisable_function
setting is a very dangerous thing to use and admins knows it, so I guess that only conscientious people will use it, but yes in theory you are right, but you won't do 10function_exists
calls for the same extension whereas a theoretically only oneextension_exists
call would be enough ?Comment #6
tstoeckler@pounard I personally actually prefer doing
extension_loaded()
alltogether, because it's much more intuitive and readable. I just wanted to bring this up as a possible argument against it. I don't feel strongly either way.Comment #7
pounardFair enough, I have no strong opinion either way too. I guess all this issue need is someone willing to take the time to propose a patch! I'm sorry but I probably won't myself.
Comment #8
cweagansThere's no patch here, and this is a pretty minor thing. Moving to 8.1.x and then this can likely be cherry picked back to 8.0.x later.
I'll look at this later today or Friday, I hope. If you haven't heard from me by then, send in the search parties :P
Comment #9
cweagansComment #22
quietone CreditAttribution: quietone as a volunteer commentedIn #bugsmash longwave suggested using Symfony polyfills for missing extensions.
So I did some grepping (
$ egrep -r "function_exists\([^\\$]" core | awk -F'function_exists' '{print $2}' | grep -v \(\) | grep -v module_ | grep -vi drupal | grep -v \'t\' | grep -v theme | awk -F\' '{ print $2 }' | sort -u
) and hand editing to create a list of parameters in function_exists that are not test or drupal specific to see if there are polyfill for them.apcu_fetch
com_create_guid
fastcgi_finish_request
ftp_connect
gd_info
gzopen
imagecreatefrompng
imagefilter
imagegd2
imagerotate
mb_ereg
opcache_invalidate
pcntl_fork
phpinfo
set_time_limit
ssh2_connect
system_test_help
uuid_create
It is too late to do more here.
Comment #23
quietone CreditAttribution: quietone as a volunteer commentedFrom the above, the extensions are
apcu - apcu_fetch
com_create_guid - ?
fastcgi_finish_request - ?
ftp - ftp_connect
gd - gd_info, imagecreatefrompng, imagefilter, imagegd2, imagerotate
mbstring - mb_ereg
opcache - opcache_invalidate
pcntl - pcntl_fork
ssh2_connect - ssh2
zlib - gzopen
Polyfills available for apcu, mbstring
And unassigning since it has been over 5 years.
Comment #24
apadernoChecking an extension is loaded instead of checking the necessary function exists assumes the function is always present for every PHP or extension version, which isn't necessarily true; extension functions can also be deprecated and removed.
IMO, the code should:
function_exists()
when the purpose is verifying a function can be called before calling itextension_loaded()
when the purpose is telling the users an extension is required for a module to workFor example, the following code should be changed.
curl_init()
could also be deprecated, and replaced by another function. That is irrelevant, to verify if the extension is loaded.Vice versa, code like the following one would be wrong.
Comment #25
apadernoTo add two more examples, I would not change this code too.
Comment #26
longwaveUnless any of the existing uses of function_exists() or extension_loaded() are known to be causing a problem e.g. in an edge case where disable_functions is used or a missing extension is polyfilled, I suggest we just leave this alone - there is no bug to fix here.
Comment #27
quietone CreditAttribution: quietone as a volunteer commentedI'm with longwave on this, "there is no bug to fix here.". Changing to a task.
As well as to leaving this alone. And as apaderno pointed out that "checking an extension is loaded instead of checking the necessary function exists assumes the function is always present for every PHP or extension version, which isn't necessarily true; extension functions can also be deprecated and removed."
Thanks to everyone who worked on resolving this issue.