summaryrefslogtreecommitdiff
path: root/Examples
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 /Examples
parent964d6274b0eba38df43d77b87c44bd728d2f0fb5 (diff)
replaces tabs in source code with regular spaces, added example for working with global variables
Diffstat (limited to 'Examples')
-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
21 files changed, 485 insertions, 384 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)