diff options
-rw-r--r-- | documentation/constructors-and-destructors.html | 4 | ||||
-rw-r--r-- | documentation/exceptions.html | 2 | ||||
-rw-r--r-- | documentation/namespaces.html | 88 |
3 files changed, 91 insertions, 3 deletions
diff --git a/documentation/constructors-and-destructors.html b/documentation/constructors-and-destructors.html index e5dbc1c..54ca23f 100644 --- a/documentation/constructors-and-destructors.html +++ b/documentation/constructors-and-destructors.html @@ -219,7 +219,7 @@ extern "C" { also use it for storing object instances. But how do you create brand new objects? For this we have the Php::Object class - which is simply an overridden Php::Value class with alternative constructors, and some additional - checks to prevent that you will ever use a Php::Object class to store values + checks to prevent that you will ever use a Php::Object object to store values other than objects. </p> <p> @@ -324,7 +324,7 @@ extern "C" { to a C++ class (allocated on the heap, with operator new!) and that turns this pointer into a PHP variable without calling the __construct() method. Notice that you must also specify the classname, - because C++ classes do not have any information about themselves (like their + because C++ classes do not hold any information about themselves (like their name), while in PHP such information is required to handle reflection and functions like get_class(). </p> diff --git a/documentation/exceptions.html b/documentation/exceptions.html index 0b87c09..f8af4d0 100644 --- a/documentation/exceptions.html +++ b/documentation/exceptions.html @@ -75,7 +75,7 @@ exception caught </p> <h2>Catching exceptions in C++</h2> <p> - But it works the other way around too. If your extensions calls a PHP + And this works the other way around too. If your extensions calls a PHP function, and that PHP function happens to throw an exception, you can catch it just as if it was a normal C++ exception. </p> diff --git a/documentation/namespaces.html b/documentation/namespaces.html new file mode 100644 index 0000000..e85e3d7 --- /dev/null +++ b/documentation/namespaces.html @@ -0,0 +1,88 @@ +<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 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 <phpcpp.h> + +// 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 an + object with exactly the same signature as the Php::Extension object, and + that you can use to register your functions. +</p> +<p> +<pre class="language-c++"><code>#include <phpcpp.h> + +// 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 + 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> |