PHP Modules Tutorials - Herong's Tutorial Examples - v5.19, by Herong Yang
dl() - Load Module Dynamically
This section provides a tutorial example on how to load a module dynamically using the dl() function.
If you have a module that is used occasionally, you can stop loading it through the .ini file. You can load it dynamically when it is needed in your PHP script by calling the dl() function. This will reduce the memory usage of the PHP runtime, if it is not needed and not loaded.
Here is an example that shows you how to call the dl() function:
<?php
# dl-load-module.php
# Copyright 2009-2026 (c) HerongYang.com. All Rights Reserved.
$module = $argv[1];
if (dl($module)) {
print("$module is loaded\n");
} else {
print("Failed to load $module\n");
}
?>
Let's try to load the "JSON" module to parse HTML documents:
herong$ php dl-load-module.php json PHP Warning: dl(): Dynamically loaded extensions aren't enabled in /Users/herong/dl-load-module.php on line 6
Looks like the dl() is disabled by default. We can enable it in the php.ini file:
herong$ php -i | grep dl /etc/php/7.2/cli/conf.d/20-readline.ini, Zend Signal Handling => enabled enable_dl => Off => Off ... herong$ sudo vi /etc/php/7.2/cli/php.ini ... enable_dl = On
Now, run the script again:
herong$ php dl-load-module.php json PHP Warning: Module 'json' already loaded in ...
Okay, I forgot to remove the loading directive for mysqli in the .ini file. Let's find the .ini file and disable the extension=json.so setting:
herong$ php --ini Configuration File (php.ini) Path: /etc/php/7.2/cli Loaded Configuration File: /etc/php/7.2/cli/php.ini Scan for additional .ini files in: /etc/php/7.2/cli/conf.d Additional .ini files parsed: /etc/php/7.2/cli/conf.d/10-mysqlnd.ini, /etc/php/7.2/cli/conf.d/10-opcache.ini, /etc/php/7.2/cli/conf.d/10-pdo.ini, /etc/php/7.2/cli/conf.d/20-calendar.ini, /etc/php/7.2/cli/conf.d/20-ctype.ini, /etc/php/7.2/cli/conf.d/20-exif.ini, /etc/php/7.2/cli/conf.d/20-fileinfo.ini, /etc/php/7.2/cli/conf.d/20-ftp.ini, /etc/php/7.2/cli/conf.d/20-gettext.ini, /etc/php/7.2/cli/conf.d/20-iconv.ini, /etc/php/7.2/cli/conf.d/20-json.ini, herong$ sudo vi /etc/php/7.2/cli/conf.d/20-json.ini ; configuration for php json module ; priority=20 ;extension=json.so
Run the script again. It should work now:
herong$ php dl-load-module.php json json is loaded
If you try to load module without its library file in the extension_dir directory, you will get an error:
herong$ php dl-load-module.php junk PHP Warning: dl(): Unable to load dynamic library 'junk' (tried: /usr/lib/php/20170718/junk (/usr/lib/php/20170718/junk: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/junk.so (/usr/lib/php/20170718/junk.so: cannot open shared object file: No such file or directory)) in /home/herong/dl-load-module.php on line 6 Failed to load junk
Table of Contents
Introduction and Installation of PHP
Managing PHP Environment and Modules on macOS
Managing PHP Environment and Modules on CentOS
Manage Runtime Configuration Directives
"php -i" - Display Configuration Information
php.ini - Configuration Directive File
Additional .ini Configuration Files
extension_dir - Module Library Location
"php -m" - Display Loaded Modules
get_extension_funcs() - Get Module Functions
►dl() - Load Module Dynamically
DOM Module - Parsing HTML Documents
GD Module - Manipulating Images and Pictures
MySQLi Module - Accessing MySQL Server
OpenSSL Module - Cryptography and SSL/TLS Toolkit
PCRE Module - Perl Compatible Regular Expressions
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples