{"id":2248,"date":"2015-09-08T07:31:52","date_gmt":"2015-09-08T07:31:52","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2248"},"modified":"2015-09-09T10:05:58","modified_gmt":"2015-09-09T10:05:58","slug":"writing-php-extensions-part-2-your-first-extension","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/08\/writing-php-extensions-part-2-your-first-extension\/","title":{"rendered":"Writing PHP Extensions &#8211; Part 2 Your First Extension"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushCpp.js\"><\/script>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/05\/writing-php-extensions-part-1-overview\/\">See Part 1 here<\/a><\/p>\n<p>We are now ready to create our first extension and as expected we will call our extension &#8220;helloworld&#8221;.<\/p>\n<p>PHP-CPP provides us with a template project to help create our own extension. You will find this in the <em>PHP-CPP\/Examples<\/em> folder . Among the several example projects, there is one called EmptyExtension. This project contains three files:<\/p>\n<ul>\n<li><em>yourextension.ini<\/em> &#8211; the file which tells PHP that your extension is available for loading<\/li>\n<li><em>Makefile<\/em> &#8211; Standard makefile for g++<\/li>\n<li><em>main.cpp<\/em> -The main c++ file where you will add your extension code<\/li>\n<li><\/li>\n<\/ul>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2250\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719-620x190.png\" alt=\"Screenshot from 2015-09-08 11:17:19\" width=\"620\" height=\"190\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719-620x190.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719-300x92.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719-900x276.png 900w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-111719.png 901w\" 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<h4>STEP 1 &#8211; Make your own folder with the example files<\/h4>\n<ul>\n<li>Copy the folder EmptyExtension and put it where your want to do your development work. Rename the folder to <em>helloworld<\/em><\/li>\n<li>Rename <em>yourextension.ini<\/em> to<em> helloworld.ini<\/em><\/li>\n<\/ul>\n<h4>STEP 2 &#8211; Changes to Makefile<\/h4>\n<ul>\n<li>Run php-config from the command prompt. Look for the line<em> &#8211;extension-dir [&lt;path to extension dir&gt;]<\/em><\/li>\n<li>Locate conf.d for your php installation. Most likely this will be in<em> \/etc\/php5\/cli\/conf.d<\/em><\/li>\n<li>Open Makefile in your editor.<\/li>\n<li>Change the line <em>NAME = yourextension<\/em> to <em>NAME = helloworld<\/em><\/li>\n<li>Change the line<em> INI_DIR = \/etc\/php5\/mods-available\/<\/em> to the path of conf.d eg. INI_DIR = \/etc\/php5\/cli\/conf.d<\/li>\n<\/ul>\n<h4>STEP 3- Changes in .ini<\/h4>\n<ul>\n<li>Open helloworld.ini in your editor<\/li>\n<li>Change the line <em>extension=yourextension.so <\/em> to <em>extension=helloworld.so<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4>STEP 4- Changes to main.cpp<\/h4>\n<p>For now we will just change the name of the extension in the sample code.<\/p>\n<ul>\n<li>Change the line <em>static Php::Extension extension(&#8220;yourextension&#8221;, &#8220;1.0&#8221;); <\/em> to<em> static Php::Extension extension(&#8220;helloworld&#8221;, &#8220;1.0&#8221;);<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>So your main.cpp should be looking as below:<br \/>\n<pre class=\"brush: cpp\">#include &lt;phpcpp.h&gt;\r\n\r\n\/**\r\n *  tell the compiler that the get_module is a pure C function\r\n *\/\r\nextern &quot;C&quot; {\r\n    \r\n    \/**\r\n     *  Function that is called by PHP right after the PHP process\r\n     *  has started, and that returns an address of an internal PHP\r\n     *  strucure with all the details and features of your extension\r\n     *\r\n     *  @return void*   a pointer to an address that is understood by PHP\r\n     *\/\r\n    PHPCPP_EXPORT void *get_module() \r\n    {\r\n        \/\/ static(!) Php::Extension object that should stay in memory\r\n        \/\/ for the entire duration of the process (that&#039;s why it&#039;s static)\r\n        static Php::Extension extension(&quot;helloworld&quot;, &quot;1.0&quot;);\r\n        \r\n        \/\/ @todo    add your own functions, classes, namespaces to the extension\r\n        \r\n        \/\/ return the extension\r\n        return extension;\r\n    }\r\n}\r\n<\/pre><\/p>\n<h4>STEP-5 COMPILE AND MAKE<\/h4>\n<p>From the command prompt, run <em>make <\/em><\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124733.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2252\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124733.png\" alt=\"Screenshot from 2015-09-08 12:47:33\" width=\"511\" height=\"73\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124733.png 511w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124733-300x43.png 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>and then <em>sudo make install<\/em><\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124832.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2253\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124832-620x97.png\" alt=\"Screenshot from 2015-09-08 12:48:32\" width=\"620\" height=\"97\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124832-620x97.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124832-300x47.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-124832.png 664w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>At this stage,<em> helloworld.so<\/em> should have been copied into your php runtime directory and in conf.d<\/p>\n<p>The runtime folder will be the same as the <em>extension dir<\/em>\u00a0 as shown in the output of php-config. which you did earlier.<\/p>\n<p>To check if the extension helloworld.so is now part of the php extension library, type <em>php -i | grep helloworld<\/em><\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2254\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216-620x103.png\" alt=\"Screenshot from 2015-09-08 12:52:16\" width=\"620\" height=\"103\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216-620x103.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216-300x50.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216-777x129.png 777w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-08-125216.png 778w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This completes the process of creating a custom PHP extension. We have made an extension, though it is not doing anything yet. In the next Part we will start adding code to the extension and see it run in your PHP code.<\/p>\n<p>Please bear in mind that currently this extension is only runnable in the CLI version of PHP and is not part of the web-server based PHP library like Apache. So only PHP scripts run from the command line will be able to use the extension. We will look into using the extension in a web environment later.<\/p>\n<h4><a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/09\/writing-php-extensions-part-3-the-four-kinds-of-functions\/\">Next: See Part 3 The Four Kinds of Functions<strong><br \/>\n<\/strong><\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>See Part 1 here We are now ready to create our first extension and as expected we will call our extension &#8220;helloworld&#8221;. PHP-CPP provides us <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/08\/writing-php-extensions-part-2-your-first-extension\/\" title=\"Writing PHP Extensions &#8211; Part 2 Your First Extension\">[&#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-2248","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\/2248","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=2248"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2248\/revisions"}],"predecessor-version":[{"id":2267,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2248\/revisions\/2267"}],"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=2248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}