The extension of PHP extensions is not much different from other C / C ++ developments. The only thing that is especially important is that you need to connect the world of PHP scripts with your C / C ++ functions. This task is not trivial, and weird macros like PHP_FUNCTION are needed here.
For example, if you want to call the "helloWorld" function from PHP, the interpreter needs to look for "helloWorld" and map it to function C. This function must have a specific signature, which means that it must take a certain set of parameters and must return a certain value. This is not arbitrary, and the interpreter cannot simply call any C / C ++ function.
This is how low-level languages such as C work, you cannot "check" what arguments the function performs.
In particular, all values in the PHP interpreter are special structures that cannot be converted to C ++ types. PHP string is different from C ++ std :: string. The PHP API provides all sorts of tools for converting values, but you need to know how to use them.
Once you know how to write a C function that can be called with PHP, you are in the “C / C ++ world”, where you can call another C / C ++ function and use C / C ++ types. Hence, just like writing clean C / C ++ applications. Well, until you have to return values back to the world of PHP, where again you have to use the PHP API tools.
There are tools that can make your life easier by helping to generate the required “glue code” for an interface with a scripting language such as PHP and “clean” C ++ projects. Check out SWIG , which is probably the clearest example.
source share