Skip to content
moriyoshi edited this page Sep 12, 2010 · 39 revisions

Boost.PHP

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”

Requirements

  • PHP version 5.1 or later.
  • Boost version 1.34.0 or later.

(Tested with g++ 3.4, 4.1 and 4.2.)

Getting Started

First you need to check out the repository to some directory (boost.php for example).

The above example can be compiled by typing the following:

g++ -DCOMPILE_DL_M002 -I. `php-config —includes` -I -g -shared -o m002.so m002.cpp

Note that PATH environment variable must contain an appropriate path to the PHP executable and php-config script.

If the compilation is successfully finishes, you’ll find the m002.so in the current working directory. Then, run the following script in the same directory with the command line below:

The script:

<?php
for ($i = 0; $i < 5; ++$i) {
    var_dump(add($i, 2), sub($i, 2));
}
?>

The command line:

php -dextension_dir=$PWD -dextension=m002.so test.php

This finally yields:


int(2)
int(-2)
int(3)
int(-1)
int(4)
int(0)
int(5)
int(1)
int(6)
int(2)
int(7)
int(3)

License

This software is released under Boost Software License 1.0 .

Clone this wiki locally