summaryrefslogtreecommitdiff
path: root/documentation/namespaces.html
blob: 0aeadbed608e7e4c6b9328aaacd494d2c0cf391a (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
84
85
86
87
88
89
<h1>Namespaces</h1>
<p>
    Although namespaces have a very rich implementation in PHP scripts,
    with special keyswords like 'use' and 'namespace' and special constants 
    like '__NAMESPACE__', they are internally very simple.
</p>
<p>
    A namespace is nothing else than a class or function prefix. If you want your classes
    or functions to appear in a specific namespace, you simply have to add 
    a prefix to the class or function name. The following code creates a
    function "myFunction" in the "myNamespace" namespace.
</p>
<p>
<pre class="language-c++"><code>#include &lt;phpcpp.h&gt;

// function that we're going to export
void myFunction()
{
}

extern "C" {
    PHPCPP_EXPORT void *get_module() {
        
        // create extension object
        static Php::Extension extension("my_extension", "1.0");
        
        // add the myFunction function to the extension, 
        // and put it in namespace "myNamespace"
        extension.add("myNamespace\\myFunction", myFunction);
        
        // return the extension
        return extension;
    }
}</code></pre>
</p>
<p>
    If you like, you can use the Php::Namespace utility class for this. This is a
    class with exactly the same signature as the Php::Extension class, and
    that you can use to register classes and functions too.
</p>
<p>
<pre class="language-c++"><code>#include &lt;phpcpp.h&gt;

// function that we're going to export
void myFunction()
{
}

extern "C" {
    PHPCPP_EXPORT void *get_module() {
        
        // create extension object
        static Php::Extension extension("my_extension", "1.0");
        
        // create a namespace
        Php::Namespace myNamespace("myNamespace");
        
        // add the myFunction function to the namespace
        myNamespace.add("myFunction", myFunction);
        
        // @todo add more functions and classes to the namespace
        
        // create a nested namespace
        Php::Namespace nestedNamespace("nestedNamespace");
        
        // @todo add functions and classes to the nested namespace
        
        // add the nested namespace to the first namespace
        myNamespace.add(std::move(nestedNamespace));
        
        // add the namespace to the extension
        extension.add(std::move(myNamespace));
        
        // return the extension
        return extension;
    }
}</code></pre>
</p>
<p>
    The Php::Namespace class is just a container that automatically adds a
    prefix to all classes and functions that you add to it. Nesting namespaces
    is possible too, as you saw in the example.
</p>
<p>
    In the example we used the std::move() function to move the nested namespace
    into the parent namespace, and to move the first namespace into the extension.
    Moving is more efficient that adding, although a regular extension.add(myNamespace) 
    would have been valid too.
</p>