{"id":2234,"date":"2015-09-05T12:48:07","date_gmt":"2015-09-05T12:48:07","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2234"},"modified":"2015-09-16T10:03:28","modified_gmt":"2015-09-16T10:03:28","slug":"writing-php-extensions-part-1-overview","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/05\/writing-php-extensions-part-1-overview\/","title":{"rendered":"Writing PHP Extensions &#8211; Part 1 Overview"},"content":{"rendered":"<h4>WHY WRITE EXTENSIONS<\/h4>\n<p>There can be 3 valid reasons why you would want to write an extension:<\/p>\n<ol>\n<li>You want to add some functionality which is either not already present in PHP or can be improved.<\/li>\n<li>You have existing code in PHP which you want to optimize in speed and its memory usage by converting it into an extension.<\/li>\n<li>You have a great piece of code in PHP which you want to distribute\/sell but do not want to give away the source.<\/li>\n<\/ol>\n<p>There could be other reasons why you want to create an extension but by and large it would be related to one of the three reasons above.<\/p>\n<h4>DEVELOPMENT LANGUAGE<\/h4>\n<p>Traditionally PHP extensions are written in C++. There are two options for development:<\/p>\n<ul>\n<li>Using the <a href=\"http:\/\/zephir-lang.com\/motivation.html\" target=\"_blank\">Zephir<\/a> language, which is a language specifically developed for writing PHP extensions. Its syntax is a mix of PHP and C and it takes care of a lot of housekeeping code and other stuff which you would have to deal with if you were to go the C++ way.<\/li>\n<li>Using <a href=\"http:\/\/www.phpcpp.com\/\" target=\"_blank\">PHP-CPP<\/a> which is\u00a0 C++ library for creating PHP extensions. It provides a much better environment and framework than using barebones C++.<\/li>\n<\/ul>\n<p>We will be using PHP-CPP for the rest of the blog series. This article assumes you are working in Linux. (I am using Linux Mint 17 Qiana). At a later stage I will add sections for Windows and FreeBSD Unix. It is also assumed that you are familiar with C++ programming.<\/p>\n<h4>SETUP PHP-DEV<\/h4>\n<p>Before you can make your extensions, you have to install the php5-dev package. To do that type<\/p>\n<p><em>sudo apt-get install php5-dev<\/em><\/p>\n<p>After installing php, you can run php-config from the shell prompt to find the directories in which the binaries and include files of PHP are installed.<\/p>\n<p>If you try to compile PHP-CPP in the next section without installing php5-dev you will get compile errors about files or folders missing.<\/p>\n<p>&nbsp;<\/p>\n<h4>DOWNLOAD AND SETUP PHP-CPP<\/h4>\n<p>PHP-CPP requires g++ version 4.8.* or later. If you have an older compiler then you should update it first.<\/p>\n<p>To get the latest version of PHP-CPP you can get it on github. But that is normally not a very stable version and you should only use it if you are already very familiar with PHP-CPP. The other option is to download a zip or tarball from their official site.<a href=\"http:\/\/www.php-cpp.com\/download\" target=\"_blank\"> This link<\/a> contains both the options.<\/p>\n<p>Make a directory and unzip the contents there. Then change working directory to the directory and run make and install.<\/p>\n<p><em>sudo make install<\/em><\/p>\n<p>On successful completion PHP-CPP should be ready for use.<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-185121.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2237\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-185121-620x361.png\" alt=\"Screenshot from 2015-09-05 18:51:21\" width=\"620\" height=\"361\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-185121-620x361.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-185121-300x175.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-185121.png 798w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Check <em>\/usr\/lib<\/em> to make sure that libphpcpp.so is present.<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-190610.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2240\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-190610-620x99.png\" alt=\"Screenshot from 2015-09-05 19:06:10\" width=\"620\" height=\"99\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-190610-620x99.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-190610-300x48.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-190610.png 732w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Check <em>\/usr\/include<\/em> to make sure phpcpp.h is present and the folder phpcpp has been created. This folder will contain all the header files of PHP-CPP.<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-191035.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2241\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-191035.png\" alt=\"Screenshot from 2015-09-05 19:10:35\" width=\"448\" height=\"224\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-191035.png 448w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-05-191035-300x150.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/08\/writing-php-extensions-part-2-your-first-extension\/\">Next: See Part 2\u00a0 Your First Extension<\/a><\/h4>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>WHY WRITE EXTENSIONS There can be 3 valid reasons why you would want to write an extension: You want to add some functionality which is <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/05\/writing-php-extensions-part-1-overview\/\" title=\"Writing PHP Extensions &#8211; Part 1 Overview\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2239,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,302],"tags":[],"class_list":["post-2234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp","category-cc"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2234"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2234\/revisions"}],"predecessor-version":[{"id":2258,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2234\/revisions\/2258"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2239"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}