summaryrefslogtreecommitdiff
path: root/README.md
blob: d468b073319591dc7bbea326ca8bbf5a95b97127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
PHP-CPP
=======

The PHP-CPP library is a C++ library for developing PHP extensions. It offers a collection
of well documented and easy-to-use classes that can be used and extended to build native
extensions for PHP.

Unlike regular PHP extensions - which are really hard to implement and require a deep
knowledge of the Zend engine and pointer manipulation - extensions built with PHP-CPP
are not difficult to develop at all. In fact, the only thing you need to do is write a function in
C++, and the PHP-CPP library uses all the power offered by C++11 to convert the parameters and return
values from your functions to/and from PHP:

<pre>
std::string hello_word()
{
    return std::string("hello world!");
}
</pre>

The function above is a native C++ function. With PHP-CPP you can export this function
to PHP with only one single C++ method call:

<pre>
extension.function("hello_world", hello_world);
</pre>

Working with parameters and return values is just as easy:

<pre>
int my_plus(int a, int b)
{
    return a+b;
}
</pre>

The PHP-CPP library ensures that the variables
from PHP (which internally are complicated C structures), are automatically converted into 
integers, passed to your function, and that the return value of your "my_plus" function is 
also converted back into a PHP variable.

Type conversion between native C/C++ types and PHP variables is handled by PHP-CPP, using
features from the C++11 language. It does not matter if your functions accept strings,
integers, booleans or other native parameters: PHP-CPP takes care of the conversion. 
The return value of your function is also transformed by PHP-CPP into PHP.

More complicated structured can be handled by PHP-CPP as well. If you would like to return
a nested associative array from your function, you can do so too:

<pre>
PhpCpp::Value get_complex_array()
{
    PhpCpp::Value r;
    r["a"] = 123;
    r["b"] = 456;
    r["c"][0] = "nested value";
    r["c"][1] = "example";
    return r;
}
</pre>

The C++ function above is equivalent to the following function in PHP:

<pre>
function get_complex_array()
{
    return array(
        "a" => 123,
        "b" => 456,
        "c" => array("nested_value","example")
    );
}
</pre>

However, this library is currently a work in progress, and it is an open
source project. We are looking for people who'd like to contribute to it.

PHP-CPP is an initiative from Copernica BV.

For more information, contact me at emiel.bruijntjes@copernica.com.


Emiel Bruijntjes (1 September 2013)