Throwing an exception instead of an error from a PHP extension

Another employee, and I greatly modified the PHP Zookeeper extension, but one thing that really bothers me is the dependency on PHP errors and exceptions.

The source is here: http://github.com/andreiz/php-zookeeper/blob/master/php_zookeeper.c#L209

Instead, it would be better to throw Zookeeper_NodeNotExists or the like, except that I have no idea what the API calls in c.

I tried a Google search and got the result set of Exceptions in the PHP language, the PHP manual does not seem to mention them, and I can’t remember which extensions for PHP packages throw an exception for you. Is there an alternative source for PHP / Zend c API documentation?

+6
c php documentation
source share
2 answers

I looked at the source code for the PHP 5.3 Sqlite extension, in particular Sqlite.c, which, as I knew, threw an exception and found

via sqlite - https://github.com/php/php-src/blob/PHP-5.3/ext/sqlite/sqlite.c#L46

#include "zend_exceptions.h" 

In zend_exceptions.h, it looks like a RuntimeException can be thrown by a simple call

 zend_throw_exception(NULL, "Some text") 

as described here https://github.com/php/php-src/blob/PHP-5.3/Zend/zend_exceptions.h#L43

The Sqlite3 extension uses it like this:

 zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC); 

where I conclude that zend_exception_get_default () gets the / handle link for RuntimeException, the second argument is an exception message, and all other work is delegated.

+7
source share

All Articles