From 6e8778d6dd0f7295cecf047e57679cba595885d1 Mon Sep 17 00:00:00 2001 From: JasperVanEck Date: Fri, 29 Nov 2013 15:11:17 +0100 Subject: Changes in exceptions, to distinguish between catching and throwing exceptions. README.md of examples is doen for now --- Examples/Exceptions/30-phpcpp.ini | 4 -- Examples/Exceptions/ExceptionCatch/30-phpcpp.ini | 4 ++ Examples/Exceptions/ExceptionCatch/Makefile | 32 +++++++++++ Examples/Exceptions/ExceptionCatch/exception.php | 17 ++++++ .../Exceptions/ExceptionCatch/exceptionCatch.cpp | 64 ++++++++++++++++++++++ Examples/Exceptions/ExceptionThrow/30-phpcpp.ini | 4 ++ Examples/Exceptions/ExceptionThrow/Makefile | 32 +++++++++++ Examples/Exceptions/ExceptionThrow/exception.php | 25 +++++++++ .../Exceptions/ExceptionThrow/exceptionThrow.cpp | 45 +++++++++++++++ Examples/Exceptions/Makefile | 32 ----------- Examples/Exceptions/exception.cpp | 48 ---------------- Examples/Exceptions/exception.php | 25 --------- Examples/README.md | 23 +++++++- 13 files changed, 245 insertions(+), 110 deletions(-) delete mode 100644 Examples/Exceptions/30-phpcpp.ini create mode 100644 Examples/Exceptions/ExceptionCatch/30-phpcpp.ini create mode 100644 Examples/Exceptions/ExceptionCatch/Makefile create mode 100644 Examples/Exceptions/ExceptionCatch/exception.php create mode 100644 Examples/Exceptions/ExceptionCatch/exceptionCatch.cpp create mode 100644 Examples/Exceptions/ExceptionThrow/30-phpcpp.ini create mode 100644 Examples/Exceptions/ExceptionThrow/Makefile create mode 100644 Examples/Exceptions/ExceptionThrow/exception.php create mode 100644 Examples/Exceptions/ExceptionThrow/exceptionThrow.cpp delete mode 100644 Examples/Exceptions/Makefile delete mode 100644 Examples/Exceptions/exception.cpp delete mode 100644 Examples/Exceptions/exception.php (limited to 'Examples') diff --git a/Examples/Exceptions/30-phpcpp.ini b/Examples/Exceptions/30-phpcpp.ini deleted file mode 100644 index 3126aa7..0000000 --- a/Examples/Exceptions/30-phpcpp.ini +++ /dev/null @@ -1,4 +0,0 @@ -; configuration for phpcpp module -; priority=30 -extension=exception.so - diff --git a/Examples/Exceptions/ExceptionCatch/30-phpcpp.ini b/Examples/Exceptions/ExceptionCatch/30-phpcpp.ini new file mode 100644 index 0000000..579fced --- /dev/null +++ b/Examples/Exceptions/ExceptionCatch/30-phpcpp.ini @@ -0,0 +1,4 @@ +; configuration for phpcpp module +; priority=30 +extension=exceptionCatch.so + diff --git a/Examples/Exceptions/ExceptionCatch/Makefile b/Examples/Exceptions/ExceptionCatch/Makefile new file mode 100644 index 0000000..26ca57c --- /dev/null +++ b/Examples/Exceptions/ExceptionCatch/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 = exceptionCatch.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/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 @@ + + * + * 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 + * + * 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 + +/** + * 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 ¶ms) +{ + // 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/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 @@ + + * + * 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 + * + * An example file to show the working of a C++ function that + * throws an exception, which can be caught by PHP. + * + */ + +/** + * Libraries used. + */ +#include + +/** + * 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(); + } +} diff --git a/Examples/Exceptions/Makefile b/Examples/Exceptions/Makefile deleted file mode 100644 index d0c44fc..0000000 --- a/Examples/Exceptions/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -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 = exception.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.cpp b/Examples/Exceptions/exception.cpp deleted file mode 100644 index 9ccd3f7..0000000 --- a/Examples/Exceptions/exception.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/** - * exception.cpp - * @author Jasper van Eck - * - * An example file to show the working of a function which throws an exception. - * - * Exceptions haven't been implemented yet. - * Therefore this example is not yet a working one. - */ - -/** - * Libraries used. - */ -#include - -/** - * Namespace to use - */ -using namespace std; - -/** - * my_throw_exception_function() - * Throws an exception. - * @return Php::Value - */ -Php::Value my_throw_exception_function() -{ - throw Php::Exception("I threw an exception in my_throw_exception_function()"); - 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 - static Php::Extension extension("my_function_throw_exception","1.0"); - - // add function to extension - extension.add("my_throw_exception_function", my_throw_exception_function); - - // return the extension module - return extension.module(); - } -} diff --git a/Examples/Exceptions/exception.php b/Examples/Exceptions/exception.php deleted file mode 100644 index 81bbd50..0000000 --- a/Examples/Exceptions/exception.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * 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/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 ¶ms) + - void my_throw_exception_function() + + + + -- cgit v1.2.3