summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
authorJasperVanEck <jaspergkurtz@gmail.com>2013-11-29 15:11:17 +0100
committerJasperVanEck <jaspergkurtz@gmail.com>2013-11-29 15:11:17 +0100
commit6e8778d6dd0f7295cecf047e57679cba595885d1 (patch)
tree5f6789f7736a846d072833550bd01e39ac4f90c3 /Examples
parentc162135cb4c49e2724d5b8028f037c01808b6e84 (diff)
Changes in exceptions, to distinguish between catching and throwing exceptions. README.md of examples is doen for now
Diffstat (limited to 'Examples')
-rw-r--r--Examples/Exceptions/ExceptionCatch/30-phpcpp.ini (renamed from Examples/Exceptions/30-phpcpp.ini)2
-rw-r--r--Examples/Exceptions/ExceptionCatch/Makefile (renamed from Examples/Exceptions/Makefile)2
-rw-r--r--Examples/Exceptions/ExceptionCatch/exception.php17
-rw-r--r--Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp64
-rw-r--r--Examples/Exceptions/ExceptionThrow/30-phpcpp.ini4
-rw-r--r--Examples/Exceptions/ExceptionThrow/Makefile32
-rw-r--r--Examples/Exceptions/ExceptionThrow/exception.php (renamed from Examples/Exceptions/exception.php)0
-rw-r--r--Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp (renamed from Examples/Exceptions/exception.cpp)13
-rw-r--r--Examples/README.md23
9 files changed, 146 insertions, 11 deletions
diff --git a/Examples/Exceptions/30-phpcpp.ini b/Examples/Exceptions/ExceptionCatch/30-phpcpp.ini
index 3126aa7..579fced 100644
--- a/Examples/Exceptions/30-phpcpp.ini
+++ b/Examples/Exceptions/ExceptionCatch/30-phpcpp.ini
@@ -1,4 +1,4 @@
; configuration for phpcpp module
; priority=30
-extension=exception.so
+extension=exceptionCatch.so
diff --git a/Examples/Exceptions/Makefile b/Examples/Exceptions/ExceptionCatch/Makefile
index d0c44fc..26ca57c 100644
--- a/Examples/Exceptions/Makefile
+++ b/Examples/Exceptions/ExceptionCatch/Makefile
@@ -9,7 +9,7 @@ PHP_CONFIG_DIR = /etc/php5/cli/conf.d
LD = g++
LD_FLAGS = -Wall -shared -O2
-RESULT = exception.so
+RESULT = exceptionCatch.so
PHPINIFILE = 30-phpcpp.ini
diff --git a/Examples/Exceptions/ExceptionCatch/exception.php b/Examples/Exceptions/ExceptionCatch/exception.php
new file mode 100644
index 0000000..06bd534
--- /dev/null
+++ b/Examples/Exceptions/ExceptionCatch/exception.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * exception.cpp
+ *
+ * @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.
+ *
+ */
+
+// call the second C++ function that accepts a callback
+my_catch_exception_function(function($a, $b, $c) {
+
+ // throw an exception from PHP - the C++ code will catch this exception
+ throw new Exception("Example exception");
+});}
diff --git a/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
new file mode 100644
index 0000000..1b95277
--- /dev/null
+++ b/Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp
@@ -0,0 +1,64 @@
+/**
+ * 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.
+ */
+
+/**
+ * Libraries used.
+ */
+#include <phpcpp.h>
+
+/**
+ * Namespace to use
+ */
+using namespace std;
+
+/**
+ * my_catch_exception_function()
+ * Catches the exception thrown by the PHP callback function.
+ * @param Php::Parameters
+ */
+void my_catch_exception_function(Php::Parameters &params)
+{
+ // the first parameter should be a callback
+ Php::Value callback = params[0];
+
+ // this must be a callable type
+ if (!callback.isCallable()) throw Php::Exception("Parameter 0 is not a function");
+
+ // we're going to call a function that could throw an exception, start a
+ // try-catch block to deal with that
+ try
+ {
+ // call the function
+ callback("some","example","parameters");
+ }
+ catch (Php::Exception &exception)
+ {
+ // @todo handle the exception that was thrown from PHP space
+ }
+}
+
+
+// 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("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();
+ }
+}
diff --git a/Examples/Exceptions/ExceptionThrow/30-phpcpp.ini b/Examples/Exceptions/ExceptionThrow/30-phpcpp.ini
new file mode 100644
index 0000000..acf8569
--- /dev/null
+++ b/Examples/Exceptions/ExceptionThrow/30-phpcpp.ini
@@ -0,0 +1,4 @@
+; configuration for phpcpp module
+; priority=30
+extension=exceptionThrow.so
+
diff --git a/Examples/Exceptions/ExceptionThrow/Makefile b/Examples/Exceptions/ExceptionThrow/Makefile
new file mode 100644
index 0000000..ec63508
--- /dev/null
+++ b/Examples/Exceptions/ExceptionThrow/Makefile
@@ -0,0 +1,32 @@
+CPP = g++
+RM = rm -f
+CPP_FLAGS = -Wall -c -I. -O2 -std=c++11
+
+PREFIX = /usr
+#Edit these lines to correspond with your own directories
+LIBRARY_DIR = ${PREFIX}/lib/php5/20121212
+PHP_CONFIG_DIR = /etc/php5/cli/conf.d
+
+LD = g++
+LD_FLAGS = -Wall -shared -O2
+RESULT = exceptionThrow.so
+
+PHPINIFILE = 30-phpcpp.ini
+
+SOURCES = $(wildcard *.cpp)
+OBJECTS = $(SOURCES:%.cpp=%.o)
+
+all: ${OBJECTS} ${RESULT}
+
+${RESULT}: ${OBJECTS}
+ ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp
+
+clean:
+ ${RM} *.obj *~* ${OBJECTS} ${RESULT}
+
+${OBJECTS}:
+ ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp}
+
+install:
+ cp -f ${RESULT} ${LIBRARY_DIR}
+ cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}
diff --git a/Examples/Exceptions/exception.php b/Examples/Exceptions/ExceptionThrow/exception.php
index 81bbd50..81bbd50 100644
--- a/Examples/Exceptions/exception.php
+++ b/Examples/Exceptions/ExceptionThrow/exception.php
diff --git a/Examples/Exceptions/exception.cpp b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
index 9ccd3f7..3719e6f 100644
--- a/Examples/Exceptions/exception.cpp
+++ b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp
@@ -2,10 +2,9 @@
* exception.cpp
* @author Jasper van Eck<jasper.vaneck@copernica.com>
*
- * An example file to show the working of a function which throws an exception.
+ * An example file to show the working of a C++ function that
+ * throws an exception, which can be caught by PHP.
*
- * Exceptions haven't been implemented yet.
- * Therefore this example is not yet a working one.
*/
/**
@@ -20,13 +19,11 @@ using namespace std;
/**
* my_throw_exception_function()
- * Throws an exception.
- * @return Php::Value
+ * Throws an exception which should be caught by PHP.
*/
-Php::Value my_throw_exception_function()
+void my_throw_exception_function()
{
throw Php::Exception("I threw an exception in my_throw_exception_function()");
- return "42";
}
@@ -37,7 +34,7 @@ extern "C"
PHPCPP_EXPORT void *get_module()
{
// create extension
- static Php::Extension extension("my_function_throw_exception","1.0");
+ static Php::Extension extension("my_exception_throw","1.0");
// add function to extension
extension.add("my_throw_exception_function", my_throw_exception_function);
diff --git a/Examples/README.md b/Examples/README.md
index 52a6251..47cdabc 100644
--- a/Examples/README.md
+++ b/Examples/README.md
@@ -121,5 +121,26 @@ FunctionWithParameters
Exceptions
----------
+
The sixth example is composed of two parts, the throw exception and
- the catch exception examples.
+ 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()
+
+
+
+