2

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.

JohnTaa
  • 2,722
  • 2
  • 15
  • 15

1 Answers1

0

Interesting question, and it still needed and answer.

You can check a PHP extension (a shared object file on linux)

  1. is a zend extension and
  2. 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.

hakre
  • 193,403
  • 52
  • 435
  • 836