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

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

Let’s revisit the example in the home:

#include "boost/php/module.hpp"
#include "boost/php/function.hpp"

using namespace boost;

class m002_module
    : public php::module,
      public php::function_container<m002_module> {
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"

This can be compiled by typing the following into the shell’s command line:

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

Or Mac OS X users do:
g++ -DCOMPILE_DL_M002 -I. `php-config —includes` -I -g -bundle -undefined dynamic_lookup -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 has successfully finished, 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)
Clone this wiki locally