From b6665b72996d23522d8a277cf4acc3d46e5e11fa Mon Sep 17 00:00:00 2001 From: JasperVanEck Date: Thu, 28 Nov 2013 16:50:12 +0100 Subject: Added a very basic exception class, and added an exception example --- Examples/Exceptions/exception.php | 5 ++-- include/exception.h | 54 ++++++++++++++++++++++++++++++++++++++ phpcpp.h | 1 + src/exception.cpp | 17 ++++++++++++ src/function.cpp | 19 +++++--------- src/includes.h | 4 ++- src/libphpcpp.so | Bin 132258 -> 132736 bytes 7 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 include/exception.h create mode 100644 src/exception.cpp diff --git a/Examples/Exceptions/exception.php b/Examples/Exceptions/exception.php index 3f567b3..b288e23 100644 --- a/Examples/Exceptions/exception.php +++ b/Examples/Exceptions/exception.php @@ -15,11 +15,12 @@ try { - echo "Function which throws an exception; my_throw_exception_function()\n" + echo "Function which throws an exception; my_throw_exception_function()\n"; echo my_throw_exception_function() . "\n"; } catch (Exception $exception) { - + echo $exception->getMessage(); } + echo my_throw_exception_function() . "\n"; diff --git a/include/exception.h b/include/exception.h new file mode 100644 index 0000000..ea328fe --- /dev/null +++ b/include/exception.h @@ -0,0 +1,54 @@ +/** + * Exception.h + * Implementation of Php Exceptions. + * + * @author Jasper van Eck + * @copyright 2013 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class Exception +{ +private: + /** + * The exception message + * @var char* + */ + char* _message; + +public: + /** + * Constructor + * @param string The exception message. + */ + Exception(char* message) throw() + { + _message = message; + } + + /** + * Destructor + */ + ~Exception() throw() + { + } + + /** + * Returns the message of the exception. + * @return std::string + */ + char* getMessage() const throw() + { + return _message; + } + +}; + +} diff --git a/phpcpp.h b/phpcpp.h index 0e2ab4a..170613f 100644 --- a/phpcpp.h +++ b/phpcpp.h @@ -42,6 +42,7 @@ #include #include #include +#include /** * Macro to export a function diff --git a/src/exception.cpp b/src/exception.cpp new file mode 100644 index 0000000..19591db --- /dev/null +++ b/src/exception.cpp @@ -0,0 +1,17 @@ +/** + * Exception.cpp + * Implementation of Php Exceptions. + * + * @author Jasper van Eck + * @copyright 2013 Copernica BV + */ + +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + + +} diff --git a/src/function.cpp b/src/function.cpp index 9376751..fe96745 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -38,20 +38,15 @@ void invoke_function(INTERNAL_FUNCTION_PARAMETERS) Parameters params(this_ptr, ZEND_NUM_ARGS()); // the function could throw an exception - //try - //{ + try + { // get the result result = function->invoke(*PHPCPP_G(environment), params); - //} - //catch (const Php::Exception &exception) - //{ - // cout << "got exception"; - - // @todo throw the exception.... - - - - //} + } + catch (const Php::Exception &exception) + { + zend_throw_exception(zend_exception_get_default(), exception.getMessage(), 0 TSRMLS_CC); + } } /** diff --git a/src/includes.h b/src/includes.h index 87b5d64..ac99950 100644 --- a/src/includes.h +++ b/src/includes.h @@ -17,6 +17,7 @@ #include #include #include +#include // for debugging #include @@ -25,7 +26,7 @@ * PHP includes */ #include - +#include "zend_exceptions.h" /** * Macro to convert results to success status */ @@ -55,6 +56,7 @@ #include "../include/class.h" #include "../include/classinfo.h" #include "../include/extension.h" +#include "../include/exception.h" #include "../include/globals.h" /** diff --git a/src/libphpcpp.so b/src/libphpcpp.so index 2c5827d..8403d89 100755 Binary files a/src/libphpcpp.so and b/src/libphpcpp.so differ -- cgit v1.2.3