Before adding the extension to php.ini file using a php script, I want to check if the extension is a zend extension just to avoid the error that happened when I load it as normal extension.
-
Any extesion name that you are targeting ? – Niklesh Raut Jun 18 '18 at 04:28
-
Zend Opcache extension – JohnTaa Jun 18 '18 at 05:03
-
For this you can use [opcache_get_status](http://php.net/manual/en/function.opcache-get-status.php) in php – Niklesh Raut Jun 18 '18 at 06:00
-
what if I want to automate this check for all extensions ? – JohnTaa Jun 20 '18 at 02:01
-
May be you have to list down all extensions and check one by one. – Niklesh Raut Jun 20 '18 at 04:11
1 Answers
Interesting question, and it still needed and answer.
You can check a PHP extension (a shared object file on linux)
- is a zend extension and
- it can be loaded from a specific PHP version from the CLI SAPI [doc].
Here a shell example asserting that a compiled .so file can be loaded:
php -n -dzend_extension="$PWD/modules/xdebug.so" \
-r 'echo implode("\n", get_loaded_extensions(true)), "\n";' \
| LC_ALL=C grep -i xdebug
This (complex) shell command returns non-zero when the file ./modules/xdebug.so
does not result in having PHP the extension named "xdebug" (case insensitive).
If you need this within a PHP script, you could use passthru()
[doc] or exec()
[doc] with the $result_code
out-parameter.
For example to test before installing the file in the extension directory [ini] and activate it in the ini.
On standard error you'll find as well diagnostic messages when it fails to load.
E.g. loading a zend extension for PHP 5.3 with PHP 5.6:
$ php5.6 -n -dzend_extension="$PWD/modules/xdebug.so" \
-r 'echo implode("\n", get_loaded_extensions(true)), "\n";' \
| LC_ALL=C grep -i xdebug
Failed loading /home/hakre/install-php5.3/xdebug-2.2.7/modules/xdebug.so:
/home/hakre/install-php5.3/xdebug-2.2.7/modules/xdebug.so: undefined symbol: php_body_write
$ echo $?
1
Some remarks:
The -n
switch reduces side-effects from configuration, it disables standard ini loading completely leaving you with the compiled in default values (you may want to run it as well without, it might show more zend extensions then).
The $PWD
[posix] is for the current working directory, it is best to pass an absolute path to the zend_extension
directive on the command-line (-dzend_extension=<extension-path>
) as with a relative path PHP will look as well in different directories if loading fails where you just want it to fail (or succeed) with exactly that binary extension file in the first place without any more look-ups.
This should give enough pointers to cover cases like upgrading an existing extension as well. It won't take the burden off you, e.g. in migration situations, to compare as well changes in an extensions ini configuration. Just saying.

- 193,403
- 52
- 435
- 836