-
Notifications
You must be signed in to change notification settings - Fork 13
Home
Boost.PHP is a set of C++ header files that allows you to create a PHP extension in C++ without any obfuscating ZEND_* / PHP_* macros, like the glorious Boost.Python after which this is loosely designed.
A typical example:
#include "boost/php/module.hpp"
#include "boost/php/function.hpp"
using namespace boost;
class m002_module
: public php::module,
public php::function_container {
public:
class handler
: public php::module::handler {
public:
handler(m002_module* mod)
:php::module::handler(mod) {}
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a – b;
}
};
public:
m002_module(zend_module_entry* entry)
: php::module(entry) {
entry→functions =
defun(“add”, &handler::add).
defun(“sub”, &handler::sub);
}
};
#define BOOST_PHP_MODULE_NAME m002
#define BOOST_PHP_MODULE_CAPITALIZED_NAME M002
#define BOOST_PHP_MODULE_VERSION “0.1”
#define BOOST_PHP_MODULE_CLASS_NAME m002_module
#include “boost/php/module_def.hpp”
The above can be compiled by typing the following:
g++ -DCOMPILE_DL_M002 \
-I.\
-I/opt/php-5.2/include/php \
-I/opt/php-5.2/include/php/main \
-I/opt/php-5.2/include/php/TSRM \
-I/opt/php-5.2/include/php/Zend \
-I/opt/php-5.2/include/php/ext \
-I/opt/php-5.2/include/php/ext/date/lib \
-g -shared -o m002.so m002.cpp
And the script to run
<?php
for ($i = 0; $i < 10; ++$i) {
var_dump(add($i, 2), sub($i, 2));
}
?>
yields:
<pre>
int(2)
int(-2)
int(3)
int(-1)
int(4)
int(0)
int(5)
int(1)
int(6)
int(2)
int(7)
int(3)
int(8)
int(4)
int(9)
int(5)
int(10)
int(6)
int(11)
int(7)
</pre>