在 PHP ZTS 環境下使用 FFI 時,沒有標準方法可以使用程式庫 (dll/so/dylib/etc) 來更改目錄,因此要解決此問題,您應該使用類似此 polyfill 的方法
<?php
$directory = 'path/to/libraries';
switch (\PHP_OS_FAMILY) {
case 'Windows':
\FFI::cdef('extern unsigned char SetDllDirectoryA(const char* lpPathName);', 'kernel32.dll')
->SetDllDirectoryA($directory)
;
break;
case 'Linux':
case 'BSD':
\FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
->setenv('LD_LIBRARY_PATH', $directory, 1)
;
break;
case 'Darwin':
\FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
->setenv('DYLD_LIBRARY_PATH', $directory, 1)
;
break;
}
?>