summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-12-07 13:12:25 -0800
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-12-07 13:12:25 -0800
commit80c8a16fb145f7ed4867d31fad3c22318a1ce708 (patch)
treef7605cdb86d653efaac761363ed596dd6b2e0459
parent964d6274b0eba38df43d77b87c44bd728d2f0fb5 (diff)
replaces tabs in source code with regular spaces, added example for working with global variables
-rw-r--r--Examples/CallPhpFunctions/callphpfunction.cpp48
-rw-r--r--Examples/CallPhpFunctions/callphpfunction.php30
-rw-r--r--Examples/CppClassesInPhp/cppclassinphp.cpp44
-rw-r--r--Examples/CppClassesInPhp/cppclassinphp.php6
-rw-r--r--Examples/Exceptions/ExceptionCatch/exception.php10
-rw-r--r--Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp38
-rw-r--r--Examples/Exceptions/ExceptionThrow/exception.php24
-rw-r--r--Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp34
-rw-r--r--Examples/Extension/extension.cpp22
-rw-r--r--Examples/Extension/extension.php6
-rw-r--r--Examples/FunctionNoParameters/functionnoparameters.cpp30
-rw-r--r--Examples/FunctionNoParameters/functionnoparameters.php6
-rw-r--r--Examples/FunctionReturnValue/functionreturnvalue.cpp30
-rw-r--r--Examples/FunctionReturnValue/functionreturnvalue.php8
-rw-r--r--Examples/FunctionVoid/functionvoid.cpp28
-rw-r--r--Examples/FunctionVoid/functionvoid.php10
-rw-r--r--Examples/FunctionWithParameters/functionwithparameters.cpp130
-rw-r--r--Examples/FunctionWithParameters/functionwithparameters.php28
-rw-r--r--Examples/Globals/globals.cpp60
-rw-r--r--Examples/Globals/globals.php29
-rw-r--r--Examples/README.md248
-rw-r--r--documentation/tutorial.html121
-rw-r--r--include/array.h83
-rw-r--r--include/exception.h78
-rw-r--r--include/extension.h2
-rw-r--r--include/globals.h18
-rw-r--r--include/value.h28
-rw-r--r--phpcpp.h1
-rw-r--r--src/includes.h1
-rw-r--r--src/value.cpp28
30 files changed, 748 insertions, 481 deletions
diff --git a/Examples/CallPhpFunctions/callphpfunction.cpp b/Examples/CallPhpFunctions/callphpfunction.cpp
index 8f212f8..9932a3e 100644
--- a/Examples/CallPhpFunctions/callphpfunction.cpp
+++ b/Examples/CallPhpFunctions/callphpfunction.cpp
@@ -1,12 +1,12 @@
/**
- * callphpfunction.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * callphpfunction.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a php function call in C++.
+ * An example file to show the working of a php function call in C++.
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <iostream>
#include <phpcpp.h>
@@ -17,37 +17,37 @@
using namespace std;
/**
- * call_php_function()
- * Calls a function in PHP space.
- * @param &params
- * @return Php::Value
+ * call_php_function()
+ * Calls a function in PHP space.
+ * @param &params
+ * @return Php::Value
*/
Php::Value call_php_function(Php::Parameters &params)
{
- // check whether the parameter is callable
- if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
-
- // perform the callback
- return params[0](1,2,3);
+ // check whether the parameter is callable
+ if (!params[0].isCallable()) throw Php::Exception("Not a callable type.");
+
+ // perform the callback
+ return params[0](1,2,3);
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("call_php_function","1.0");
// add function to extension
extension.add("call_php_function", call_php_function, {
- Php::ByVal("addFunc", Php::callableType),
- Php::ByVal("x", Php::numericType)
- });
-
- // return the extension module
- return extension.module();
- }
+ Php::ByVal("addFunc", Php::callableType),
+ Php::ByVal("x", Php::numericType)
+ });
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/CallPhpFunctions/callphpfunction.php b/Examples/CallPhpFunctions/callphpfunction.php
index 77d633c..8da284d 100644
--- a/Examples/CallPhpFunctions/callphpfunction.php
+++ b/Examples/CallPhpFunctions/callphpfunction.php
@@ -1,33 +1,33 @@
<?php
/**
- * callphpfunction.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * callphpfunction.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a php function call in C++.
+ * An example file to show the working of a php function call in C++.
*/
class MyClass
{
- function method($a,$b,$c)
- {
- return $a+$b+$c;
- }
+ function method($a,$b,$c)
+ {
+ return $a+$b+$c;
+ }
}
function myFunction($a,$b,$c)
{
- return $a+$b+$c;
+ return $a+$b+$c;
}
/**
- * Call a C++ function with a callable PHP function as its parameter.
- * The PHP function is then executed from the C++ code.
- * The PHP function is this case, adds three numbers.
+ * Call a C++ function with a callable PHP function as its parameter.
+ * The PHP function is then executed from the C++ code.
+ * The PHP function is this case, adds three numbers.
*/
echo(call_php_function(function($a, $b, $c){
- return $a + $b + $c;
- })."\n");
-
+ return $a + $b + $c;
+ })."\n");
+
echo(call_php_function("myFunction")."\n");
-
+
echo(call_php_function(array(new MyClass(), 'method'))."\n");
diff --git a/Examples/CppClassesInPhp/cppclassinphp.cpp b/Examples/CppClassesInPhp/cppclassinphp.cpp
index fffa655..2894fdc 100644
--- a/Examples/CppClassesInPhp/cppclassinphp.cpp
+++ b/Examples/CppClassesInPhp/cppclassinphp.cpp
@@ -1,8 +1,8 @@
/**
- * cppclassinphp.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * cppclassinphp.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of using a C++ class in PHP.
+ * An example file to show the working of using a C++ class in PHP.
*/
#include "includeMyCustomClass.h"
@@ -40,32 +40,32 @@ public:
}
void myMethod(Php::Parameters &params)
- {
- std::cout << "myMethod is called." << std::endl;
- std::cout << "_x: " << _x << std::endl;
- _x = params[0];
- std::cout << "New _x" << _x << std::endl;
- }
+ {
+ std::cout << "myMethod is called." << std::endl;
+ std::cout << "_x: " << _x << std::endl;
+ _x = params[0];
+ std::cout << "New _x" << _x << std::endl;
+ }
};
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_function_with_parameters","1.0");
// add the custom class ot the extension
- extension.add("MyClass", Php::Class<MyCustomClass>({
- Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod),{
- Php::ByVal("newX", Php::numericType)
- })
- }));
-
- // return the extension module
- return extension.module();
- }
+ extension.add("MyClass", Php::Class<MyCustomClass>({
+ Php::Public("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod),{
+ Php::ByVal("newX", Php::numericType)
+ })
+ }));
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/CppClassesInPhp/cppclassinphp.php b/Examples/CppClassesInPhp/cppclassinphp.php
index bea276c..1eccf95 100644
--- a/Examples/CppClassesInPhp/cppclassinphp.php
+++ b/Examples/CppClassesInPhp/cppclassinphp.php
@@ -1,9 +1,9 @@
<?php
/**
- * cppclassinphp.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * cppclassinphp.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of using a C++ class in PHP.
+ * An example file to show the working of using a C++ class in PHP.
*/
//create a MyCustomClass object, which is an object of a C++ class
diff --git a/Examples/Exceptions/ExceptionCatch/exception.php b/Examples/Exceptions/ExceptionCatch/exception.php
index 06bd534..0fec704 100644
--- a/Examples/Exceptions/ExceptionCatch/exception.php
+++ b/Examples/Exceptions/ExceptionCatch/exception.php
@@ -1,12 +1,12 @@
<?php
/**
- * exception.cpp
+ * exception.cpp
*
- * @author Jasper van Eck <jasper.vaneck@copernica.com>
+ * @author Jasper van Eck <jasper.vaneck@copernica.com>
*
- * This example shows the working of a C++ function that throws an
- * exception, and that exception is then caught by PHP code.
- *
+ * This example shows the working of a C++ function that throws an
+ * exception, and that exception is then caught by PHP code.
+ *
*/
// call the second C++ function that accepts a callback
diff --git a/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
index 1b95277..a5f7b96 100644
--- a/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
+++ b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
@@ -1,14 +1,14 @@
/**
- * exception.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * exception.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a C++ function that
- * takes a callback function as parameter, and handles the
- * exception thrown by the callback function.
+ * An example file to show the working of a C++ function that
+ * takes a callback function as parameter, and handles the
+ * exception thrown by the callback function.
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <phpcpp.h>
@@ -18,9 +18,9 @@
using namespace std;
/**
- * my_catch_exception_function()
- * Catches the exception thrown by the PHP callback function.
- * @param Php::Parameters
+ * my_catch_exception_function()
+ * Catches the exception thrown by the PHP callback function.
+ * @param Php::Parameters
*/
void my_catch_exception_function(Php::Parameters &params)
{
@@ -47,18 +47,18 @@ void my_catch_exception_function(Php::Parameters &params)
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_exception_catch","1.0");
// add function to extension
extension.add("my_catch_exception_function", my_catch_exception_function, {
- Php::ByVal("callback", Php::callableType);
- });
-
- // return the extension module
- return extension.module();
- }
+ Php::ByVal("callback", Php::callableType);
+ });
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/Exceptions/ExceptionThrow/exception.php b/Examples/Exceptions/ExceptionThrow/exception.php
index 81bbd50..3afe86f 100644
--- a/Examples/Exceptions/ExceptionThrow/exception.php
+++ b/Examples/Exceptions/ExceptionThrow/exception.php
@@ -1,25 +1,25 @@
<?php
/**
- * exception.cpp
+ * exception.cpp
*
- * @author Jasper van Eck <jasper.vaneck@copernica.com>
+ * @author Jasper van Eck <jasper.vaneck@copernica.com>
*
- * This example shows the working of a C++ function that throws an
- * exception, and that exception is then caught by PHP code.
- *
+ * This example shows the working of a C++ function that throws an
+ * exception, and that exception is then caught by PHP code.
+ *
*/
// try-catch block to be able to handle the exception
try
{
- // the my_throw_exception function is implemented in C++. and
- // it is going to throw an exception
- my_throw_exception_function();
+ // the my_throw_exception function is implemented in C++. and
+ // it is going to throw an exception
+ my_throw_exception_function();
}
catch (Exception $exception)
{
- // the exception object is thrown by C++ code, and caught by PHP
- // code
- echo $exception->getMessage(). "\n";
-
+ // the exception object is thrown by C++ code, and caught by PHP
+ // code
+ echo $exception->getMessage(). "\n";
+
}
diff --git a/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
index 3719e6f..d104047 100644
--- a/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
+++ b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
@@ -1,14 +1,14 @@
/**
- * exception.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * exception.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a C++ function that
- * throws an exception, which can be caught by PHP.
- *
+ * An example file to show the working of a C++ function that
+ * throws an exception, which can be caught by PHP.
+ *
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <phpcpp.h>
@@ -18,28 +18,28 @@
using namespace std;
/**
- * my_throw_exception_function()
- * Throws an exception which should be caught by PHP.
+ * my_throw_exception_function()
+ * Throws an exception which should be caught by PHP.
*/
void my_throw_exception_function()
{
- throw Php::Exception("I threw an exception in my_throw_exception_function()");
+ throw Php::Exception("I threw an exception in my_throw_exception_function()");
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_exception_throw","1.0");
// add function to extension
extension.add("my_throw_exception_function", my_throw_exception_function);
-
- // return the extension module
- return extension.module();
- }
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/Extension/extension.cpp b/Examples/Extension/extension.cpp
index 9f7f495..99da5c7 100644
--- a/Examples/Extension/extension.cpp
+++ b/Examples/Extension/extension.cpp
@@ -1,8 +1,8 @@
/**
- * extension.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * extension.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of an extension.
+ * An example file to show the working of an extension.
*/
// library include
#include <phpcpp.h>
@@ -15,13 +15,13 @@ using namespace std;
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_simple_extension","1.0");
-
- // return the extension module
- return extension.module();
- }
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/Extension/extension.php b/Examples/Extension/extension.php
index 3941c8c..6f6b03c 100644
--- a/Examples/Extension/extension.php
+++ b/Examples/Extension/extension.php
@@ -1,9 +1,9 @@
<?php
/*
- * extension.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * extension.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of an extension.
+ * An example file to show the working of an extension.
*/
// print all the extensions currently loaded.
diff --git a/Examples/FunctionNoParameters/functionnoparameters.cpp b/Examples/FunctionNoParameters/functionnoparameters.cpp
index da645fd..8934169 100644
--- a/Examples/FunctionNoParameters/functionnoparameters.cpp
+++ b/Examples/FunctionNoParameters/functionnoparameters.cpp
@@ -1,12 +1,12 @@
/**
- * functionnoparameters.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionnoparameters.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call without parameters.
+ * An example file to show the working of a function call without parameters.
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <phpcpp.h>
@@ -16,28 +16,28 @@
using namespace std;
/**
- * my_no_parameters_function()
- * @return Php::Value
+ * my_no_parameters_function()
+ * @return Php::Value
*/
Php::Value my_no_parameters_function()
{
- return "42";
+ return "42";
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_function_return_value","1.0");
// add function to extension
extension.add("my_no_parameters_function", my_no_parameters_function);
-
- // return the extension module
- return extension.module();
- }
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/FunctionNoParameters/functionnoparameters.php b/Examples/FunctionNoParameters/functionnoparameters.php
index f4035ae..9c62845 100644
--- a/Examples/FunctionNoParameters/functionnoparameters.php
+++ b/Examples/FunctionNoParameters/functionnoparameters.php
@@ -1,9 +1,9 @@
<?php
/**
- * functionnoparameters.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionnoparameters.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call without parameters.
+ * An example file to show the working of a function call without parameters.
*/
diff --git a/Examples/FunctionReturnValue/functionreturnvalue.cpp b/Examples/FunctionReturnValue/functionreturnvalue.cpp
index 890a15f..a107fe7 100644
--- a/Examples/FunctionReturnValue/functionreturnvalue.cpp
+++ b/Examples/FunctionReturnValue/functionreturnvalue.cpp
@@ -1,12 +1,12 @@
/**
- * functionreturnvalue.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionreturnvalue.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call with a return value.
+ * An example file to show the working of a function call with a return value.
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <phpcpp.h>
@@ -16,28 +16,28 @@
using namespace std;
/**
- * my_return_value_function()
- * @return Php::Value
+ * my_return_value_function()
+ * @return Php::Value
*/
Php::Value my_return_value_function()
{
- return "42";
+ return "42";
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_function_return_value","1.0");
// add function to extension
extension.add("my_return_value_function", my_return_value_function);
-
- // return the extension module
- return extension.module();
- }
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/FunctionReturnValue/functionreturnvalue.php b/Examples/FunctionReturnValue/functionreturnvalue.php
index 1626e19..dc3e68e 100644
--- a/Examples/FunctionReturnValue/functionreturnvalue.php
+++ b/Examples/FunctionReturnValue/functionreturnvalue.php
@@ -1,12 +1,12 @@
<?php
/**
- * functionreturnvalue.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionreturnvalue.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call with a return value.
+ * An example file to show the working of a function call with a return value.
*/
/**
- * Run a function which returns a value.
+ * Run a function which returns a value.
*/
echo my_return_value_function() . "\n";
diff --git a/Examples/FunctionVoid/functionvoid.cpp b/Examples/FunctionVoid/functionvoid.cpp
index f4386ea..91b7bb3 100644
--- a/Examples/FunctionVoid/functionvoid.cpp
+++ b/Examples/FunctionVoid/functionvoid.cpp
@@ -1,12 +1,12 @@
/**
- * functionvoid.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionvoid.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a void function call.
+ * An example file to show the working of a void function call.
*/
/**
- * Libraries used.
+ * Libraries used.
*/
#include <iostream>
#include <phpcpp.h>
@@ -17,27 +17,27 @@
using namespace std;
/**
- * my_function_void()
+ * my_function_void()
*/
void my_function_void()
{
- cout << "In my_function_void()" << endl;
+ cout << "In my_function_void()" << endl;
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_function_void","1.0");
// add function to extension
extension.add("my_void_function", my_function_void);
-
- // return the extension module
- return extension.module();
- }
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/FunctionVoid/functionvoid.php b/Examples/FunctionVoid/functionvoid.php
index e6fbb67..d057f92 100644
--- a/Examples/FunctionVoid/functionvoid.php
+++ b/Examples/FunctionVoid/functionvoid.php
@@ -1,13 +1,13 @@
<?php
/*
- * functionvoid.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionvoid.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a void function call.
+ * An example file to show the working of a void function call.
*/
/*
- * Run the function with the given name. As you can see, the given name
- * can be different from the actual function name.
+ * Run the function with the given name. As you can see, the given name
+ * can be different from the actual function name.
*/
my_void_function();
diff --git a/Examples/FunctionWithParameters/functionwithparameters.cpp b/Examples/FunctionWithParameters/functionwithparameters.cpp
index 4f70f26..dd7bb9f 100644
--- a/Examples/FunctionWithParameters/functionwithparameters.cpp
+++ b/Examples/FunctionWithParameters/functionwithparameters.cpp
@@ -1,19 +1,19 @@
/**
- * functionwithparameters.cpp
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionwithparameters.cpp
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call with parameters.
+ * An example file to show the working of a function call with parameters.
*/
/**
- * Default Cpp libraries
+ * Default Cpp libraries
*/
#include <string>
#include <iostream>
/**
- * Our own library.
+ * Our own library.
*/
#include <phpcpp.h>
@@ -23,102 +23,102 @@
using namespace std;
/**
- * my_with_undefined_parameters_function()
- * @param Php:Parameters the given parameters
+ * my_with_undefined_parameters_function()
+ * @param Php:Parameters the given parameters
*/
void my_with_undefined_parameters_function(Php::Parameters &params)
{
- for (unsigned int i = 0; i < params.size(); i++)
- {
- cout << "Parameter " << i << ": " << params[i] << endl;
- }
+ for (unsigned int i = 0; i < params.size(); i++)
+ {
+ cout << "Parameter " << i << ": " << params[i] << endl;
+ }
}
/**
- * my_with_defined_parameters_function()
- * @param Php::Parameters the given parameters
- * @return Php::Value Param[0] and Param[1] added
+ * my_with_defined_parameters_function()
+ * @param Php::Parameters the given parameters
+ * @return Php::Value Param[0] and Param[1] added
*/
Php::Value my_with_defined_parameters_function(Php::Parameters &params)
{
- for (unsigned int i = 0; i < params.size(); i++)
- {
- cout << "Parameter " << i << ": " << params[i] << endl;
- }
-
- return params[0] + params[1];
+ for (unsigned int i = 0; i < params.size(); i++)
+ {
+ cout << "Parameter " << i << ": " << params[i] << endl;
+ }
+
+ return params[0] + params[1];
}
/**
- * This functions receives a reference to a variable. When the variable is altered,
- * so is the value in the php script.
- * my_with_defined_parameters_reference_function()
- * @param Php::Parameters the given parameters
+ * This functions receives a reference to a variable. When the variable is altered,
+ * so is the value in the php script.
+ * my_with_defined_parameters_reference_function()
+ * @param Php::Parameters the given parameters
*/
void my_with_defined_parameters_reference_function(Php::Parameters &params)
{
- params[0] = "I changed!";
+ params[0] = "I changed!";
}
/**
- * my_with_defined_parameters_reference_function()
- * @param Php::Parameters the given parameters
+ * my_with_defined_parameters_reference_function()
+ * @param Php::Parameters the given parameters
*/
void my_with_defined_array_parameters_function(Php::Parameters &params)
{
- for (int i = 0; i < params[0].size(); i++)
- {
- cout << "The array: " << params[0][i] << endl;
- }
+ for (int i = 0; i < params[0].size(); i++)
+ {
+ cout << "The array: " << params[0][i] << endl;
+ }
}
/**
- * my_with_defined_object_parameters_function()
+ * my_with_defined_object_parameters_function()
* The use of objects is not yet implemented.
- * @param Php::Parameters the given parameters
+ * @param Php::Parameters the given parameters
*/
void my_with_defined_object_parameters_function(Php::Parameters &params)
{
- for (unsigned int i = 0; i < params.size(); i++)
- {
- cout << params[i] << endl;
- }
+ for (unsigned int i = 0; i < params.size(); i++)
+ {
+ cout << params[i] << endl;
+ }
}
// Symbols are exported according to the "C" language
extern "C"
{
- // export the "get_module" function that will be called by the Zend engine
- PHPCPP_EXPORT void *get_module()
- {
- // create extension
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
static Php::Extension extension("my_function_with_parameters","1.0");
// add function, with undefined parameters, to extension
extension.add("my_with_undefined_parameters_function", my_with_undefined_parameters_function);
-
- // add function, with defined numeric parameters, to extension
- extension.add("my_with_defined_parameters_function", my_with_defined_parameters_function, {
- Php::ByVal("x", Php::numericType),
+
+ // add function, with defined numeric parameters, to extension
+ extension.add("my_with_defined_parameters_function", my_with_defined_parameters_function, {
+ Php::ByVal("x", Php::numericType),
Php::ByVal("y", Php::numericType)
- });
-
- // add function, with defined parameter by reference, to extension
- extension.add("my_with_defined_parameters_reference_function", my_with_defined_parameters_reference_function, {
- Php::ByRef("string", Php::stringType)
- });
-
- // add function, with defined array parameter, to extension
- extension.add("my_with_defined_array_parameters_function", my_with_defined_array_parameters_function, {
- Php::ByVal("array", Php::arrayType)
- });
-
- // add function, with defined object parameter, to extension
- extension.add("my_with_defined_object_parameters_function", my_with_defined_object_parameters_function, {
- Php::ByVal("myClassObjVar", "MyPhpClass")
- });
-
- // return the extension module
- return extension.module();
- }
+ });
+
+ // add function, with defined parameter by reference, to extension
+ extension.add("my_with_defined_parameters_reference_function", my_with_defined_parameters_reference_function, {
+ Php::ByRef("string", Php::stringType)
+ });
+
+ // add function, with defined array parameter, to extension
+ extension.add("my_with_defined_array_parameters_function", my_with_defined_array_parameters_function, {
+ Php::ByVal("array", Php::arrayType)
+ });
+
+ // add function, with defined object parameter, to extension
+ extension.add("my_with_defined_object_parameters_function", my_with_defined_object_parameters_function, {
+ Php::ByVal("myClassObjVar", "MyPhpClass")
+ });
+
+ // return the extension module
+ return extension.module();
+ }
}
diff --git a/Examples/FunctionWithParameters/functionwithparameters.php b/Examples/FunctionWithParameters/functionwithparameters.php
index 0ca360e..b1c3198 100644
--- a/Examples/FunctionWithParameters/functionwithparameters.php
+++ b/Examples/FunctionWithParameters/functionwithparameters.php
@@ -1,17 +1,17 @@
<?php
/*
- * functionwithparameters.php
- * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ * functionwithparameters.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function call with parameters, defined and undefined.
+ * An example file to show the working of a function call with parameters, defined and undefined.
*/
/*
- * Test class.
+ * Test class.
*/
class MyPhpClass {
- public $aMemberVar = "aMemberVar";
+ public $aMemberVar = "aMemberVar";
public function __toString()
{
@@ -20,12 +20,12 @@ class MyPhpClass {
public function getMemberVar()
{
- return $aMemberVar;
- }
+ return $aMemberVar;
+ }
}
/*
- * Run a function with parameters.
+ * Run a function with parameters.
*/
@@ -33,20 +33,20 @@ class MyPhpClass {
* A function which takes parameters, which are all undefined;
* my_with_undefined_parameters_function('1st','2nd','3rd','4th')
*/
-echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n";
+echo my_with_undefined_parameters_function('1st','2nd','3rd','4th') . "\n\n\n";
/*
* A function which takes parameters, which are all defined;
* my_with_defined_parameters_function(21,42)
*/
-
-echo my_with_defined_parameters_function(21,42) . "\n\n\n";
+
+echo my_with_defined_parameters_function(21,42) . "\n\n\n";
/*
* A function which takes a reference of a parameter
* my_with_defined_parameters_reference_function(referenceVar)
*/
-
+
$referenceVar = "I am unchanged.";
echo "The value of referenceVar: " . $referenceVar. "\n";
@@ -65,9 +65,9 @@ echo my_with_defined_array_parameters_function($myArray) . "\n\n\n";
* A function which takes an object as a parameter
* my_with_defined_object_parameter_function(myPhpClass)
*/
-
+
$myPhpClass = new MyPhpClass;
-echo my_with_defined_object_parameters_function($myPhpClass);
+echo my_with_defined_object_parameters_function($myPhpClass);
/*
* Accessing a non-existant parameters index will result in a segmentation fault.
diff --git a/Examples/Globals/globals.cpp b/Examples/Globals/globals.cpp
new file mode 100644
index 0000000..6fa9eea
--- /dev/null
+++ b/Examples/Globals/globals.cpp
@@ -0,0 +1,60 @@
+/**
+ * functionvoid.cpp
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ *
+ * An example file to show how global variables can be accessed
+ */
+
+/**
+ * Libraries used.
+ */
+#include <iostream>
+#include <phpcpp.h>
+
+/**
+ * Namespace to use
+ */
+using namespace std;
+
+/**
+ * process_globals()
+ *
+ * This function reads and modifies global variables
+ */
+Php::Value process_globals()
+{
+ // all global variables can be accessed via the Php::globals variable,
+ // which is more or less the same as the PHP $_GLOBALS variable
+
+ // set a global variable
+ Php::globals["a"] = 1;
+
+ // increment a global variable
+ Php::globals["b"] += 1;
+
+ // set a global variable to be an array
+ Php::globals["c"] = Php::Array();
+
+ // add a member to an array
+ Php::globals["c"]["member"] = 123;
+
+ // if a global variable holds a function, we can call it
+ return Php::globals["d"](1,2,3);
+}
+
+// Symbols are exported according to the "C" language
+extern "C"
+{
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
+ static Php::Extension extension("globals","1.0");
+
+ // add function to extension
+ extension.add("process_globals", process_globals);
+
+ // return the extension module
+ return extension.module();
+ }
+}
diff --git a/Examples/Globals/globals.php b/Examples/Globals/globals.php
new file mode 100644
index 0000000..36febf6
--- /dev/null
+++ b/Examples/Globals/globals.php
@@ -0,0 +1,29 @@
+<?php
+/*
+ * globals.php
+ * @author Jasper van Eck<jasper.vaneck@copernica.com>
+ *
+ * An example file to show the working of a void function call.
+ */
+
+// we first need to set a number of globals
+$b = 10;
+$d = function($a,$b,$c) {
+ return $a+$b+$c;
+};
+
+// call the C++ function that will do something
+$d = process_globals();
+
+// the global variable $a should not have the value 1
+echo("a = $a\n");
+
+// the variable $b should not be 11
+echo("b = $b\n");
+
+// $c should be an array
+echo("c['member'] = ".$c['member']."\n");
+
+// $d is overwritten and now is 6
+echo("d = $d\n");
+
diff --git a/Examples/README.md b/Examples/README.md
index 5bd9e1e..d29673a 100644
--- a/Examples/README.md
+++ b/Examples/README.md
@@ -1,148 +1,160 @@
This directory contains a number of examples that show how to use the
PHP-CPP library.
-To run an example, there are several steps which need to be taken.
+To run an example, there are a couple of steps that need to be taken.
The first step is compiling and installing the PHPCPP library. This is
-done running 'make' and then 'make install' in the main directory.
-The second step is compiling the C++ code and made into an extension
-usable by PHP. This is done by running 'make' and 'make install'
-in an Example directory. Do make sure you've edited the Makefile
-according to your own specific directories.
+done by running 'make' and then 'make install' in the main directory.
+
+The second step is to compile the C++ code of an example and make it
+into an extension usable by PHP. This is done by running 'make' and
+'make install' in an Example directory. Do make sure you've edited the
+Makefile according to your own specific directories.
The following examples are available:
### [Extension](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Extension)
- The first example does nothing - it only shows how to create your
- own extension. This means your extension will be listed in the
- output of "phpinfo()", and it is included in the array returned
- by theget_loaded_modules() function.
-
- There are no functions or classes defined by this first example
- extension.
-
-
+ The first example does nothing - it only shows how to create your
+ own extension. This means your extension will be listed in the
+ output of "phpinfo()", and it is included in the array returned
+ by theget_loaded_modules() function.
+
+ There are no functions or classes defined by this first example
+ extension.
+
+
### [FunctionVoid](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionVoid)
- This second example shows how to add a function to the extension
- and call that function from the PHP code. Adding a function to
- your extension means that you can call it anywhere from the PHP
- code.
-
- Furthermore, it is possible to associate your C++ function with
- another name. This other name is then used in PHP to call the C++
- function, rather than the original C++ function name.
-
- Functions and/or classes defined in this example.
- - void my_function_void() Named as my_void_function()
+ This second example shows how to add a function to the extension
+ and call that function from the PHP code. Adding a function to
+ your extension means that you can call it anywhere from the PHP
+ code.
+
+ Furthermore, it is possible to associate your C++ function with
+ another name. This other name is then used in PHP to call the C++
+ function, rather than the original C++ function name.
+
+ Functions and/or classes defined in this example.
+ - void my_function_void() Named as my_void_function()
### [FunctionReturnValue](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionReturnValue)
- The third example shows how to return a value from C++ to PHP.
- Virtually any type of value can be returned to PHP from C++.
- The returned value must be returned as Php::Value, rather than
- its own type. This Php::Value can then be used in your PHP code.
-
- Because a Php::Value is always returned, there is no need to specify
- the return type of the function when adding it to your extension.
-
- Functions and/or classes defined in this example.
- - Php::Value my_return_value_function()
-
+ The third example shows how to return a value from C++ to PHP.
+ Virtually any type of value can be returned to PHP from C++.
+ The returned value must be returned as Php::Value object, rather
+ than a native C/C++ type. This Php::Value class takes care of
+ converting native values into values usable in your PHP code.
+
+ Because a Php::Value is always returned, there is no need to specify
+ the return type of the function when adding it to your extension.
+
+ Functions and/or classes defined in this example.
+ - Php::Value my_return_value_function()
+
### [FunctionNoParameters](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionNoParameters)
- The fourth example is a combination of the second and third example.
- This example illustrates how to call a function without parameters.
- The function is added to your extension, and can then be called from
- your PHP script.
-
- The function returns a Php::Value to show that the call succeeded.
-
- Functions and/or classes defined in this example.
- - Php::Value my_no_parameters_function()
-
-
+ The fourth example is a combination of the second and third example.
+ This example illustrates how to call a function without parameters.
+ The function is added to your extension, and can then be called from
+ your PHP script.
+
+ The function returns a Php::Value to show that the call succeeded.
+
+ Functions and/or classes defined in this example.
+ - Php::Value my_no_parameters_function()
+
+
### [FunctionWithParameters](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/FunctionWithParameters)
- The fifth example is an example to show how several different types
- of parameters can used for functions. There are two ways to pass a
- parameter, by value(Php::ByVal) and by reference(Php::ByRef). Each
- take two parameters of their own. The first being the parameter name,
- and the second the parameter type.
-
- Furthermore, parameters are always stored in the Php::Parameters
- object. This object is basicly an array which hold all the parameters,
- in order.
-
- The first option being the undefined parameters. With undefined
- parameters, we can pass any and as many parameters as we want to
- the function.
-
- The second option is defining each parameter when adding the function
- to your extension. In this case we have added two Php::numericType
- parameters to the function. In 'type.h' you can find all avaiable
- types, however not every type has been implemented yet.
-
- The third option is passing a reference of a variable. Meaning when
- it is altered in the C++ code, its value will also change in the PHP
- code. This can achieved by using Php:ByRef, rather than Php::ByVal.
-
- The fourth option is passing an array as parameter. The array
- parameter will be accessible from the N-1 index of the
- Php::Parameters object, where is the argument number of the array
- when passing it to the function.
-
- The fifth and final option is passing an object. An object can be
- passed in the same way as any other data type, except for that
- you must specify what the class is of the object. This can be done
- by passing a string with the class name as the second parameter to
- Php::ByVal or Php::ByRef.
-
- Functions and/or classes defined in this example.
- 1. void my_with_undefined_parameters_function(Php::Parameters &params)
- 2. Php::Value my_with_defined_parameters_function(Php::Parameters &params)
- 3. void my_with_defined_parameters_reference_function(Php::Parameters &params)
- 4. void my_with_defined_array_parameters_function(Php::Parameters &params)
- 5. void my_with_defined_object_parameters_function(Php::Parameters &params)
+ The fifth example is an example to show how several different types
+ of parameters can used for functions. There are two ways to pass a
+ parameter, by value(Php::ByVal) and by reference(Php::ByRef). Each
+ take two parameters of their own. The first being the parameter name,
+ and the second the parameter type.
+
+ Furthermore, parameters are always stored in the Php::Parameters
+ object. This object is basicly an array which hold all the parameters,
+ in order.
+
+ The first option being the undefined parameters. With undefined
+ parameters, we can pass any and as many parameters as we want to
+ the function.
+
+ The second option is defining each parameter when adding the function
+ to your extension. In this case we have added two Php::numericType
+ parameters to the function. In 'type.h' you can find all avaiable
+ types, however not every type has been implemented yet.
+
+ The third option is passing a reference of a variable. Meaning when
+ it is altered in the C++ code, its value will also change in the PHP
+ code. This can achieved by using Php:ByRef, rather than Php::ByVal.
+
+ The fourth option is passing an array as parameter. The array
+ parameter will be accessible from the N-1 index of the
+ Php::Parameters object, where is the argument number of the array
+ when passing it to the function.
+
+ The fifth and final option is passing an object. An object can be
+ passed in the same way as any other data type, except for that
+ you must specify what the class is of the object. This can be done
+ by passing a string with the class name as the second parameter to
+ Php::ByVal or Php::ByRef.
+
+ Functions and/or classes defined in this example.
+ 1. void my_with_undefined_parameters_function(Php::Parameters &params)
+ 2. Php::Value my_with_defined_parameters_function(Php::Parameters &params)
+ 3. void my_with_defined_parameters_reference_function(Php::Parameters &params)
+ 4. void my_with_defined_array_parameters_function(Php::Parameters &params)
+ 5. void my_with_defined_object_parameters_function(Php::Parameters &params)
+
+
+### [Globals](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Globals)
+
+ Global PHP variables can be used accessed from your C++ code. You
+ can do this by accessing the Php::values array, which more or less
+ behaves the same as the $_GLOBALS array does in PHP.
+
+ Functions and/or classes defined in this example.
+ 1. void process_globals()
### [Exceptions](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/Exceptions)
- The sixth example is composed of two parts, the throw exception and
- the catch exception examples. The requirements of the catch example,
- passing a callback as a parameter, have not yet been implemented.
-
- The throw example is there to show that an exception thrown in
- a C++ function can be caught and handled in your PHP script. The
- exception thrown is a Php::Exception.
-
- The catch example shows that when a PHP function is passed as a
- callback, and is capable of throwing a (PHP) exception, that it
- can be caught as Php::Exception and then handled in the C++ code.
- However, the passing of a function as a callback has not yet been
- implemented. It would need to be implemented for this specific
- example to work.
-
- Functions and/or classes defined in this example.
- - void my_catch_exception_function(Php::Parameters &params)
- - void my_throw_exception_function()
+ The sixth example is composed of two parts, the throw exception and
+ the catch exception examples. The requirements of the catch example,
+ passing a callback as a parameter, have not yet been implemented.
+
+ The throw example is there to show that an exception thrown in
+ a C++ function can be caught and handled in your PHP script. The
+ exception thrown is a Php::Exception.
+
+ The catch example shows that when a PHP function is passed as a
+ callback, and is capable of throwing a (PHP) exception, that it
+ can be caught as Php::Exception and then handled in the C++ code.
+ However, the passing of a function as a callback has not yet been
+ implemented. It would need to be implemented for this specific
+ example to work.
+
+ Functions and/or classes defined in this example.
+ - void my_catch_exception_function(Php::Parameters &params)
+ - void my_throw_exception_function()
### [PHP function calls](https://github.com/EmielBruijntjes/PHP-CPP/tree/master/Examples/CallPhpFunctions)
- The seventh example shows how to pass a callable PHP function as
- a parameter. As can be seen in the example, there are several ways
- of passing a PHP function to the C++ function. When a function is
- passed, it is possible to use the () operator on the parameter, with
- the correct amount of parameters for the callable PHP function.
-
- When using the wrong amount, or when trying to use the () operator
- on a non-callable type, you will get PHP errors rather than
- segmentation faults or other kinds of C++ errors.
-
- Functions and/or classes defined in this example.
- - Php::Value call_php_function(Php::Parameters &params)
+ The seventh example shows how to pass a callable PHP function as
+ a parameter. As can be seen in the example, there are several ways
+ of passing a PHP function to the C++ function. When a function is
+ passed, it is possible to use the () operator on the parameter, with
+ the correct amount of parameters for the callable PHP function.
+
+ When using the wrong amount, or when trying to use the () operator
+ on a non-callable type, you will get PHP errors rather than
+ segmentation faults or other kinds of C++ errors.
+
+ Functions and/or classes defined in this example.
+ - Php::Value call_php_function(Php::Parameters &params)
diff --git a/documentation/tutorial.html b/documentation/tutorial.html
index 0e0a23e..30d8ea7 100644
--- a/documentation/tutorial.html
+++ b/documentation/tutorial.html
@@ -1,10 +1,28 @@
-<h1>Loading native extensions</h1>
+<div style="width: 1024px; font-family: verdana; font-size: 10pt; line-height: 16pt;">
+
+
+<h1>A PHP-CPP tutorial</h1>
<p>
- Native PHP extensions are compiled into *.so files, and can be enabled by adding
- a line to the core php.ini configuration file - or one of the additional
- configuration files that are loaded by PHP. If you do not know where you can
- find the PHP configuration file(s) on your system, you can run the following
- command from the command line:
+ Building PHP extensions with the PHP-CPP library is not at all difficult -
+ in this tutorial we will give you step by step instructions to build
+ your own extensions. However, before we start the actual programming, we will
+ first explain how PHP interacts with its extensions - how extension libraries
+ are loaded and how PHP knows which functions, constants and classes are
+ defined by the extensions.
+</p>
+<h2>How does PHP load its extensions?</h2>
+<p>
+ You probably already know that native PHP extensions are compiled into *.so
+ files on unix-like systems, and *.dll files on Windows environments, and that
+ the global php.ini file holds a list of all extensions available on your system.
+ This means that if you're building your own extension, you will also need to
+ create such a *.so or *.dll file and you will need to update the PHP
+ configuration so that your own extension is loaded by PHP.
+</p>
+<h3>Where to find your PHP configuration files?</h3>
+<p>
+ If for one reason or another you can not find the PHP configuration file(s)
+ on your system, you can run the following command from the command line:
</p>
<p>
<code><pre>
@@ -12,6 +30,7 @@
</pre></code>
</p>
<p>
+ This will output a list of all configuration files that are loaded by PHP.
Extensions are enabled by adding "extension=name.so" lines to the
configuration file - where 'name' should of course be replaced by the name of
your extension. A default PHP installation already comes with many default
@@ -30,13 +49,16 @@
php -i|grep extension_dir
</pre></code>
</p>
+<h2>The get_module() startup function</h2>
<p>
- When PHP starts, it loads its configuration file(s) and for each "extension=name.so"
- line in it, it will open the appropriate library, and call the "get_module()"
- function from it. Each extension library must therefore define and implement
- this "get_module()" C function. The function is called by PHP right after
- the library is loaded (and thus way before pageviews are handled), and it
- should return a memory address that points to a structure that holds information
+ Before we explain how you can create your own extension however, we first explain
+ what PHP does to load an extension. When PHP starts, it loads the configuration
+ file(s) that we just described and for each "extension=name.so" line in these
+ files, it opens the appropriate library, and calls the "get_module()"
+ function from it. Each extension library (your extension too) must therefore
+ define and implement this "get_module()" C function. The function is called by
+ PHP right after the library is loaded (and thus way before pageviews are handled),
+ and it should return a memory address that points to a structure that holds information
about all functions, classes, variables and constants that are made available
by the extension.
</p>
@@ -60,7 +82,9 @@
</p>
<p>
In the example above you see a very straightforward implementation of the
- get_module() function. A number of elements require special attention. For a
+ get_module() function. Every PHP extension that uses the PHP-CPP library
+ implements this function in a more or less similar way as the extension
+ starting point. A number of elements require special attention. For a
start, the only header file that you see is the phpcpp.h header
file. If you're using the PHP-CPP library to build your own extensions,
you do not have to include the complicated, unstructured, and mostly undocumented
@@ -69,8 +93,9 @@
files of the core PHP engine - but you do not have to.
</p>
<p>
- The PHP-CPP library is a C++ library, and you can use all features
- of this language. However, PHP expects your library, and especially your
+ The next thing that you'll notice it that we placed the get_module() function
+ inside an 'extern "C"' code block. As the name of the library already gives away,
+ PHP-CPP is a C++ library. However, PHP expects your library, and especially your
get_module() function, to be implemented in C and not in C++. That's why we've
wrapped the get_module() function in an 'extern "C"' block. This will instruct
the C++ compiler that the get_module() is a regular C function, and
@@ -85,19 +110,19 @@
<p>
Inside the get_module() function the Extension object is instantiated, and the
Extension::module() method is called. It is crucial that you make a <i>static</i>
- instance of this Extension class, because the object should exist for the entire
+ instance of this Extension class, because the object must exist for the entire
lifetime of the PHP process, and not only for the duration of the get_module()
call. The constructor takes two arguments: the name of your extension and
its version number. The Extension::module() method returns the memory address
that PHP needs to initialize the library.
</p>
<p>
- Note that the example above does not yet make any native functions or
- native classes available in PHP - it only creates the extension.
+ Note that the example above does not yet export any native functions or
+ native classes to PHP - it only creates the extension.
</p>
-<h1>Exporting native functions</h1>
+<h2>Exporting native functions</h2>
<p>
- An extension is of course only useful if you define functions and/or
+ An extension can of course only be useful if you define functions and/or
classes that can be accessed from PHP scripts. For functions you can do this
by adding your native function implementations to the Extension object:
</p>
@@ -106,9 +131,9 @@
#include &lt;phpcpp.h&gt;
extern void example1();
- extern void example2(Php::Parameters &params);
+ extern void example2(Php::Parameters &amp;params);
extern Php::Value example3();
- extern Php::Value example4(Php::Parameters &params);
+ extern Php::Value example4(Php::Parameters &amp;params);
extern "C" {
PHPCPP_EXPORT void *get_module() {
@@ -158,13 +183,57 @@
</pre></code>
</p>
<p>
- The signature of the four example functions are exactly the signatures that
- are supported by the PHP-CPP libraries. Every native function that returns
- void or a Php::Value object, and that either accepts a Php::Parameters object
+ It is not possible to export every thinkable C/C++ function to the
+ PHP extension. Only functions that have one of the four supported signatures
+ can be exported: functions that return
+ void or a Php::Value object, and that either accept a Php::Parameters object
or no parameters at all, can be added to the extension object and can thus
be exported to PHP.
</p>
-<h1>Working with variables</h1>
+<h2>Parameter types</h2>
+<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>
+ <code><pre>
+ #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::numericType),
+ Php::ByVal("b", "ExampleClass"),
+ Php::ByRef("c", "OtherClass")
+ });
+ return myExtension.module();
+ }
+ }
+ </pre></code>
+</p>
+<p>
+ Above you see that we pass in additional information when we register the
+
+
+
+ The Extension::add() method can be used to register native functions, and
+ make them available in PHP. In the examples above, you've seen that the
+ method takes two parameters: the name the function shou
+
+
+<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.
diff --git a/include/array.h b/include/array.h
new file mode 100644
index 0000000..6956fee
--- /dev/null
+++ b/include/array.h
@@ -0,0 +1,83 @@
+/**
+ * Array.h
+ *
+ * An array is an extension to the Value class. It extends the Value class
+ * to initialize the variable as an array, instead of a null pointer
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Array : public Value
+{
+public:
+ /**
+ * Constructor
+ */
+ Array() : Value() { setType(arrayType); }
+
+ /**
+ * Copy constructor
+ * @param array
+ */
+ Array(const Array &array) : Value(array) {}
+
+ /**
+ * Move constructor
+ * @param array
+ */
+ Array(Array &&that) : Value(std::move(that)) {}
+
+ /**
+ * Copy constructor from a value object
+ * @param value
+ */
+ Array(const Value &value) : Value(value) { setType(arrayType); }
+
+ /**
+ * Destructor
+ */
+ virtual ~Array() {}
+
+ /**
+ * Change the internal type of the variable
+ * @param Type
+ */
+ virtual Value &setType(Type type) override
+ {
+ // only possible for arrays
+ if (type != arrayType) return *this;
+
+ // call base
+ return Value::setType(type);
+ }
+
+protected:
+ /**
+ * Validate the object
+ * @return Value
+ */
+ virtual Value &validate() override
+ {
+ // make sure the value object is an array
+ setType(arrayType);
+
+ // call base
+ return Value::validate();
+ }
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/exception.h b/include/exception.h
index ccb6b08..061c106 100644
--- a/include/exception.h
+++ b/include/exception.h
@@ -1,9 +1,9 @@
/**
- * Exception.h
- * Implementation of Php Exceptions.
+ * Exception.h
+ * Implementation of Php Exceptions.
*
- * @author Jasper van Eck <jasper.vaneck@copernica.com>
- * @copyright 2013 Copernica BV
+ * @author Jasper van Eck <jasper.vaneck@copernica.com>
+ * @copyright 2013 Copernica BV
*/
#include <exception>
@@ -18,42 +18,42 @@ namespace Php {
class Exception : public std::exception
{
private:
- /**
- * The exception message
- * @var char*
- */
- std::string _message;
-
- /**
- * The PHP exception code
- * @var int
- */
- int _code;
-
+ /**
+ * The exception message
+ * @var char*
+ */
+ std::string _message;
+
+ /**
+ * The PHP exception code
+ * @var int
+ */
+ int _code;
+
public:
- /**
- * Constructor
- * @param &string
- */
- Exception(const std::string &message, int code = 0) : std::exception(), _message(message), _code(code)
- {
- }
-
- /**
- * Destructor
- */
- virtual ~Exception()
- {
- }
-
- /**
- * Returns the message of the exception.
- * @return &string
- */
- std::string &message() throw()
- {
- return _message;
- }
+ /**
+ * Constructor
+ * @param &string
+ */
+ Exception(const std::string &message, int code = 0) : std::exception(), _message(message), _code(code)
+ {
+ }
+
+ /**
+ * Destructor
+ */
+ virtual ~Exception()
+ {
+ }
+
+ /**
+ * Returns the message of the exception.
+ * @return &string
+ */
+ std::string &message() throw()
+ {
+ return _message;
+ }
};
/**
diff --git a/include/extension.h b/include/extension.h
index 3555fb6..862549a 100644
--- a/include/extension.h
+++ b/include/extension.h
@@ -35,7 +35,7 @@ class Extension;
/**
* Optional callback types for starting and stopping the request
- * @param extension
+ * @param extension
*/
typedef bool (*request_callback)(Extension *extension);
diff --git a/include/globals.h b/include/globals.h
index b3ee6c3..6faeb8a 100644
--- a/include/globals.h
+++ b/include/globals.h
@@ -1,11 +1,11 @@
/**
* Globals.h
*
- * Wrapper object that gives access to all global variables. You
+ * Wrapper object that gives access to all global variables. You
* can use it more or less the same as the $_GLOBALS object in
- * PHP.
+ * PHP.
*
- * The global PHP variables are acessible via the Php::globals["varname"]
+ * The global PHP variables are acessible via the Php::globals["varname"]
* variables.
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
@@ -87,16 +87,16 @@ private:
Globals() {}
public:
- /**
- * Get the one and only instance
- * @return Globals
- */
- static Globals &instance();
+ /**
+ * Get the one and only instance
+ * @return Globals
+ */
+ static Globals &instance();
};
/**
* We always have one instance
- * @var Globals
+ * @var Globals
*/
extern Globals &globals;
diff --git a/include/value.h b/include/value.h
index b2ce89b..09392ad 100644
--- a/include/value.h
+++ b/include/value.h
@@ -264,7 +264,7 @@ public:
* Change the internal type of the variable
* @param Type
*/
- Value &setType(Type type);
+ virtual Value &setType(Type type);
/**
* Make a clone of the value with the same type
@@ -290,7 +290,7 @@ public:
bool isFloat() const { return type() == floatType; }
bool isObject() const { return type() == objectType; }
bool isArray() const { return type() == arrayType; }
- bool isCallable() const;
+ bool isCallable() const;
/**
* Retrieve the value as number
@@ -531,7 +531,7 @@ public:
/**
* Call the function in PHP
* We have ten variants of this function, depending on the number of parameters
- * This call operator is only useful when the variable represents a callable
+ * This call operator is only useful when the variable represents a callable
* @param name Name of the function
* @return Value
*/
@@ -548,13 +548,13 @@ public:
Value operator()(Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9);
private:
- /**
- * Call function with a number of parameters
- * @param argc Number of parameters
- * @param argv The parameters
- * @return Value
- */
- Value exec(int argc, struct _zval_struct ***params);
+ /**
+ * Call function with a number of parameters
+ * @param argc Number of parameters
+ * @param argv The parameters
+ * @return Value
+ */
+ Value exec(int argc, struct _zval_struct ***params);
protected:
/**
@@ -564,6 +564,14 @@ protected:
struct _zval_struct *_val;
/**
+ * Validate the value
+ * This is a overridable function that is implemented in base classes to
+ * ensure that a value of certain type stays valid
+ * @return Value
+ */
+ virtual Value &validate() { return *this; }
+
+ /**
* The Globals and Member classes can access the zval directly
*/
friend class Globals;
diff --git a/phpcpp.h b/phpcpp.h
index 0f99a5e..236a93a 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -24,6 +24,7 @@
*/
#include <phpcpp/type.h>
#include <phpcpp/value.h>
+#include <phpcpp/array.h>
#include <phpcpp/hiddenpointer.h>
#include <phpcpp/globals.h>
#include <phpcpp/argument.h>
diff --git a/src/includes.h b/src/includes.h
index ae8956f..18e9951 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -37,6 +37,7 @@
*/
#include "../include/type.h"
#include "../include/value.h"
+#include "../include/array.h"
#include "../include/hiddenpointer.h"
#include "../include/globals.h"
#include "../include/argument.h"
diff --git a/src/value.cpp b/src/value.cpp
index b0c3350..194934d 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -299,7 +299,7 @@ Value &Value::operator=(Value &&value)
}
// update the object
- return *this;
+ return validate();
}
/**
@@ -345,7 +345,7 @@ Value &Value::operator=(const Value &value)
}
// update the object
- return *this;
+ return validate();
}
/**
@@ -365,7 +365,7 @@ Value &Value::operator=(int16_t value)
ZVAL_LONG(_val, value);
// update the object
- return *this;
+ return validate();
}
/**
@@ -385,7 +385,7 @@ Value &Value::operator=(int32_t value)
ZVAL_LONG(_val, value);
// update the object
- return *this;
+ return validate();
}
/**
@@ -405,7 +405,7 @@ Value &Value::operator=(int64_t value)
ZVAL_LONG(_val, value);
// update the object
- return *this;
+ return validate();
}
/**
@@ -425,7 +425,7 @@ Value &Value::operator=(bool value)
ZVAL_BOOL(_val, value);
// update the object
- return *this;
+ return validate();
}
/**
@@ -445,7 +445,7 @@ Value &Value::operator=(char value)
ZVAL_STRINGL(_val, &value, 1, 1);
// update the object
- return *this;
+ return validate();
}
/**
@@ -465,7 +465,7 @@ Value &Value::operator=(const std::string &value)
ZVAL_STRINGL(_val, value.c_str(), value.size(), 1);
// update the object
- return *this;
+ return validate();
}
/**
@@ -485,7 +485,7 @@ Value &Value::operator=(const char *value)
ZVAL_STRING(_val, value, 1);
// update the object
- return *this;
+ return validate();
}
/**
@@ -505,7 +505,7 @@ Value &Value::operator=(double value)
ZVAL_DOUBLE(_val, value);
// update the object
- return *this;
+ return validate();
}
/**
@@ -971,8 +971,6 @@ long Value::numericValue() const
// already a long?
if (isNumeric()) return Z_LVAL_P(_val);
- std::cout << "clone to numeric" << std::endl;
-
// make a clone
return clone(numericType).numericValue();
}
@@ -1208,6 +1206,9 @@ const Value &Value::set(int index, const Value &value)
// the variable has one more reference (the array entry)
Z_ADDREF_P(value._val);
+ // object should stay valid
+ validate();
+
// done
return value;
}
@@ -1243,6 +1244,9 @@ const Value &Value::set(const char *key, int size, const Value &value)
// the variable has one more reference (the array entry)
Z_ADDREF_P(value._val);
+ // object should stay valid
+ validate();
+
// done
return value;
}