We are happy to announce that we have added PHP 7 bindings to our PDF SDK through SWIG 3. This blog will outline some of the changes we made to our PHP PDF library so that it would support both PHP5/SWIG2 and PHP7/SWIG3.

linkAPIs and Data Structures

There are some major changes in APIs and data structures between PHP 5 and PHP 7. In order to support PHP 7, we have made some changes to PDFNetPHP.i and PDFNetUStringPHP.i in PDFNetPHP:

linkPHP 5:

zval **data
Z_ARRVAL_PP, Z_TYPE_PP
zend_hash_get_current_data_ex(_,_,_)
ZVAL_STRINGL(_,_,_,_)

linkPHP 7:

zval *data
Z_ARRVAL_P, Z_TYPE_P
zend_hash_get_current_data_ex(_,_)
ZVAL_STRINGL(_,_,_)

linkCMakeLists.txt

We also made the following changes to CMakeLists.txt:

linkPHP 5:

execute_process(
COMMAND ${SWIG_EXECUTABLE} -c++ -php -DNTS -DSWIGHIDDEN_SIG -I${PDFNetC_Include_Dir} -outdir ${PDFNetPHP_SourcesDir} -o ${PDFNetPHP_SourcesDir}/PDFNetPHP.cpp -oh ${PDFNetPHP_SourcesDir}/PDFNetPHP.hpp PDFNetPHP.i
)

linkPHP 7:

execute_process(
COMMAND ${SWIG_EXECUTABLE} -c++ -php7 -DNTS -DSWIGHIDDEN_SIG -I${PDFNetC_Include_Dir} -outdir ${PDFNetPHP_SourcesDir} -o ${PDFNetPHP_SourcesDir}/PDFNetPHP.cpp -oh ${PDFNetPHP_SourcesDir}/PDFNetPHP.hpp PDFNetPHP.i
)

linkA Combined Solution

Instead of having separate solutions for PHP 5 and PHP 7, we were able to combine them into a single build by using a php-config executable in CMake to obtain PHP package information:

find_program(PHP_CONFIG_EXECUTABLE NAMES php-config7 php-config5 php-config4 php-config)
execute_process(
COMMAND
    ${PHP_CONFIG_EXECUTABLE} --vernum
    OUTPUT_STRIP_TRAILING_WHITESPACE
    COMMAND sed -ne "s/....$//p"
    OUTPUT_VARIABLE PHP_MAJOR_VERSION
)

Once PHP_MAJOR_VERSION is obtained, we can set the appropriate arguments on SWIG:

if (PHP_MAJOR_VERSION LESS 7)
       set(php_version "php")
else()
       set(php_version "php7")
endif()
execute_process(
    COMMAND ${SWIG_EXECUTABLE} -c++ -${php_version})

The next changes are using preprocessor directives in the interface files:

#if PHP_MAJOR_VERSION >= 7
...
#else
... 
#endif

More details about the implementation can be found here. The following images show the resulting PHP5 and PHP7 builds on two different machines: PHP 5.5.9/SWIG 2.0.11 (Ubuntu 14.04, CMake 2.8.12)

PHP 7.2.10/SWIG 3.0.12 (Ubuntu 18.04, Cmake 3.10.2)

For more information about setup instructions, please click here.

Conclusion

In this blog, we outlined how we added support for PHP 7 without the need for a separate configuration, while also ensuring seamless support for any newer PHP/SWIG versions. You can give it a try by signing up for a free trial. If you have any questions, please feel free to contact us and we will be happy to help.