diff options
Diffstat (limited to 'Examples/Exceptions/ExceptionThrow')
-rw-r--r-- | Examples/Exceptions/ExceptionThrow/30-phpcpp.ini | 4 | ||||
-rw-r--r-- | Examples/Exceptions/ExceptionThrow/Makefile | 32 | ||||
-rw-r--r-- | Examples/Exceptions/ExceptionThrow/exception.php | 25 | ||||
-rw-r--r-- | Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp | 45 |
4 files changed, 106 insertions, 0 deletions
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/ExceptionThrow/exception.php b/Examples/Exceptions/ExceptionThrow/exception.php new file mode 100644 index 0000000..81bbd50 --- /dev/null +++ b/Examples/Exceptions/ExceptionThrow/exception.php @@ -0,0 +1,25 @@ +<?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. + * + */ + +// 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(); +} +catch (Exception $exception) +{ + // 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 new file mode 100644 index 0000000..3719e6f --- /dev/null +++ b/Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp @@ -0,0 +1,45 @@ +/** + * 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. + * + */ + +/** + * Libraries used. + */ +#include <phpcpp.h> + +/** + * Namespace to use + */ +using namespace std; + +/** + * 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()"); +} + + +// 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_throw","1.0"); + + // add function to extension + extension.add("my_throw_exception_function", my_throw_exception_function); + + // return the extension module + return extension.module(); + } +} |