summaryrefslogtreecommitdiff
path: root/documentation/parameters.html
blob: 464d855d0fbc8714ff6f2a5ea6a06d745b3b43bf (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
<h1>Limiting function parameters</h1>
<p>
    PHP has a mechanism to enforce function parameters types, and to accept
    parameters either by reference or by value. In the examples above, we have
    not yet used that mechanism yes: it is up to the function implementations
    themselves to inspect the 'Parameters' object, and check if the
    variables are of the right type.
</p>
<p>
    However, the 'Extension::add()' method takes a third optional parameter that
    you can use to specify the number of parameters that are supported, whether
    the parameters are passed by reference or by value, and what the type of
    the parameters is:
</p>
<p>
<pre class="language-c++"><code>
#include &lt;phpcpp.h&gt;

extern void example(Php::Parameters &amp;params);

extern "C" {
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("my_extension", "1.0");
        myExtension.add("example", example, {
            Php::ByVal("a", Php::Type::Numeric),
            Php::ByVal("b", "ExampleClass"),
            Php::ByRef("c", "OtherClass")
        });
        return myExtension.module();
    }
}
</pre></code>
</p>
<p>
    Above you see that we passed in additional information when we registered the
    "example" function. We tell our extension that our function accepts three parameters:
    the first parameter must be a regular number, while the other ones are object 
    instances of type "ExampleClass" and "OtherClass". In the end, your native C++ 
    "example" function will still be called with a Php::Parameters instance, but 
    the moment it gets called, you can be sure that the Php::Parameters object 
    will be filled with three members, and that two of them are objects of the 
    appropriate type, and that the third one is also passed by reference.
</p>
<h2>Working with variables</h2>
<p>
    Variables in PHP are non-typed. A variable can thus hold any possible type:
    an integer, string, a floating point number, and even an object or an array.
    C++ on the other hand is a typed language. In C++ an integer variable always 
    has a numeric value, and a string variable always hold a string value.
</p>
<p>
    When you mix native code and PHP code, you will need to convert the non-typed 
    PHP variables into native variables, and the other way round: convert native 
    variables back into non-typed PHP variables. The PHP-CPP library offers the 
    "Value" class that makes this a very simple task.
</p>
<p>