Search code examples
phpapachephp-extension

Loading a PHP extension at runtime results in Uncaught Error: Call to undefined function dl()


I am using PHP 7.1.11 I am just trying this function dl(), but it shows an error.

I commented php_fileinfo.dll in php.ini and restarted the Apache. I think the problem is not with .dll and so, the problem is with the dl() function.

Code:

<?php
error_reporting(-1);
echo phpversion();
   
echo PHP_OS;
if(!extension_loaded('fileinfo')){
    dl('php_fileinfo.dll');
}
...

Error thrown:

7.1.11WINNT Fatal error: Uncaught Error: Call to undefined function dl() in D:\xampp\htdocs\php\1.php:7

Stack trace: #0 {main} thrown in D:\xampp\htdocs\php\1.php on line 7


Solution

  • The problem is that dl() function was removed from most SAPIs some time ago and you are asked to use the Extension Loading Directives instead.

    The current PHP Documentation warns exactly about this.

    1. Documentation and history

    Warning This function was removed from most SAPIs in PHP 5.3.0, and was removed from PHP-FPM in PHP 7.0.0.

    This is the full change history:

    • 7.0.0 dl() is disabled in PHP-FPM.
    • 5.3.9 dl() is enabled in PHP-FPM, albeit discouraged.
    • 5.3.0 dl() is now disabled in some SAPIs due to stability issues. The only SAPIs that allow dl() are CLI and Embed.

    2. Is there a solution?

    The real question is do you really need a solution? The dl() functions was deprecated for a reason after all.

    It's up to your system to support your codebase and not your codebase to make the system compliant.

    Use the extension_loaded() function to check if the extension is loaded and if not raise a error/exception instead of trying to dynamically load it in runtime.

    You can load all the extensions you want securely by using the system's php.ini file extension directives