{"id":67,"date":"2008-07-03T10:28:31","date_gmt":"2008-07-03T18:28:31","guid":{"rendered":"http:\/\/www.codebelay.com\/blog\/?p=67"},"modified":"2008-07-03T12:35:31","modified_gmt":"2008-07-03T20:35:31","slug":"how-to-make-your-own-php-extension-quick-and-dirty","status":"publish","type":"post","link":"https:\/\/www.codebelay.com\/blog\/2008\/07\/03\/how-to-make-your-own-php-extension-quick-and-dirty\/","title":{"rendered":"How to Make Your Own PHP Extension&#8230; Quick and Dirty"},"content":{"rendered":"<p>This is a cheat sheet on how to extend <a href=\"http:\/\/www.php.net\/\">PHP<\/a>. I&#8217;m learning how to fix bugs in C within the PHP source code right now using what&#8217;s google-able, the #php chat room in irc.freenode.net and <a href=\"http:\/\/www.amazon.com\/Extending-Embedding-PHP-Developers-Library\/dp\/067232704X\/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1215117256&#038;sr=8-1\">&#8220;Extending and Embedding PHP&#8221;<\/a> by <a href=\"http:\/\/blog.libssh2.org\/\">Sara Goleman<\/a>.<\/p>\n<p>Preliminaries: make sure you have the right tools for building php from source. Right now these tools are:<\/p>\n<ul>\n<li>autoconf: 2.13<\/li>\n<li>automake: 1.4+<\/li>\n<li>libtool: 1.4.x+ (except 1.4.2)<\/li>\n<li>bison: 1.28, 1.35, 1.75, 2.0 or higher<\/li>\n<li>flex: 2.5.4 (not higher)<\/li>\n<li>re2c: 0.9.11+ (0.12.0+ for HEAD)<\/li>\n<\/ul>\n<ul>\n<li>Get the latest php stable version. as of this blog posting it&#8217;s <a href=\"http:\/\/us3.php.net\/get\/php-5.2.6.tar.bz2\/from\/a\/mirror\">php-5.2.6<\/a><\/li>\n<li>unpack the file using something like bunzip2 -c < php-5.2.6.tar.bz2 | tar xvf -<\/li>\n<li>.\/configure &#8211;prefix=\/usr\/local &#8211;enable-debug (I&#8217;m really old school and like to put my stuff in \/usr\/local . YMMV.)<\/li>\n<li>make ; make install<\/li>\n<\/ul>\n<p>If that worked now we&#8217;re ready to make a module. Feel free to substitute barce or BARCE in the code below with whatever you want the module name to be.<\/p>\n<ul>\n<li>cd ext (if you type ls you&#8217;ll see the extensions that come with php.)<\/li>\n<li>mkdir barce (I&#8217;m a narcissistic, buddhist, nihilist.)<\/li>\n<li>create a config.m4 file with this code:<\/li>\n<\/ul>\n<pre>\r\nPHP_ARG_ENABLE(barce,\r\n        [Whether to enable the \"barce\" extension],\r\n        [  --enable-barce       Enable \"barce\" extension support])\r\n\r\nif test $PHP_BARCE != \"no\"; then\r\n        PHP_SUBST(BARCE_SHARED_LIBADD)\r\n        PHP_NEW_EXTENSION(barce, barce.c, $ext_shared)\r\nfi\r\n<\/pre>\n<ul>\n<li>then create a php_barce.h file:<\/li>\n<\/ul>\n<pre>\r\n#ifndef PHP_BARCE_H\r\n\/* Prevent double inclusion *\/\r\n#define PHP_BARCE_H\r\n\r\n\/* Define extension properties *\/\r\n#define PHP_BARCE_EXTNAME \"barce\"\r\n#define PHP_BARCE_EXTVER \"1.0\"\r\n\r\n\/* Import configure options\r\n * when building outside of the \r\n * PHP source tree *\/\r\n#ifdef HAVE_CONFIG_H\r\n#include \"config.h\"\r\n#endif\r\n\r\n\/* Include PHP standard Header *\/\r\n#include \"php.h\"\r\n\/*\r\n * define the entry point symbole\r\n * Zend will use when loading this module\r\n *\/\r\nextern zend_module_entry barce_module_entry;\r\n#define phpext_barce_ptr &barce_module_entry\r\n\r\n#endif \/* PHP_BARCE_H *\/\r\n\r\n<\/pre>\n<ul>\n<li>create a barce.c file with your functions that you are creating for PHP<\/li>\n<\/ul>\n<pre>\r\n#include \"php_barce.h\"\r\n\r\nPHP_FUNCTION(barce_thinks_youre_cool)\r\n{\r\n        char *name;\r\n        int name_len;\r\n\r\n        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \"s\", \r\n                &name, &name_len) == FAILURE)\r\n        {\r\n                RETURN_NULL();\r\n        }\r\n\r\n        php_printf(\"Barce thinks you're cool, \");\r\n        PHPWRITE(name, name_len);\r\n        php_printf(\"!\\n\");\r\n}\r\n\r\n\r\nstatic function_entry php_barce_functions[] = {\r\n        PHP_FE(barce_thinks_youre_cool, NULL)\r\n        { NULL, NULL, NULL }\r\n};\r\n\r\nzend_module_entry barce_module_entry = {\r\n#if ZEND_MODULE_API_NO >= 20010901\r\n        STANDARD_MODULE_HEADER,\r\n#endif\r\n        PHP_BARCE_EXTNAME,\r\n        php_barce_functions, \/* Functions *\/\r\n        NULL, \/* MINIT *\/\r\n        NULL, \/* MSHUTDOWN *\/\r\n        NULL, \/* RINIT *\/\r\n        NULL, \/* RSHUTDOWN *\/\r\n        NULL, \/* MINFO *\/\r\n#if ZEND_MODULE_API_NO >= 20010901\r\n        PHP_BARCE_EXTVER,\r\n#endif\r\n        STANDARD_MODULE_PROPERTIES\r\n};\r\n\r\n#ifdef COMPILE_DL_BARCE\r\nZEND_GET_MODULE(barce)\r\n#endif\r\n\r\n<\/pre>\n<p>Now let&#8217;s build this puppy.<\/p>\n<ul>\n<li>phpize<\/li>\n<li>.\/configure &#8211;enable-barce<\/li>\n<li>make<\/li>\n<li>there should now be a modules directory. copy it to \/usr\/local\/lib\/php\/modules\/ .<\/li>\n<li>edit php.ini and add two lines:\n<ul>\n<li> extension_dir = &#8220;\/usr\/local\/lib\/php\/modules&#8221;<\/li>\n<li>extension = &#8220;barce.so&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Let&#8217;s test and see if it worked:<\/p>\n<p>php -r &#8220;barce_thinks_youre_cool(&#8216;Put your name here&#8217;);&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a cheat sheet on how to extend PHP. I&#8217;m learning how to fix bugs in C within the PHP source code right now using what&#8217;s google-able, the #php chat room in irc.freenode.net and &#8220;Extending and Embedding PHP&#8221; by Sara Goleman. Preliminaries: make sure you have the right tools for building php from source. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,33],"tags":[90,391,91],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-how-to","category-webapps","tag-c","tag-php","tag-php-extension"],"_links":{"self":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":0,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}