{"id":2259,"date":"2015-09-09T10:03:07","date_gmt":"2015-09-09T10:03:07","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2259"},"modified":"2015-09-10T11:49:56","modified_gmt":"2015-09-10T11:49:56","slug":"writing-php-extensions-part-3-the-four-kinds-of-functions","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/09\/writing-php-extensions-part-3-the-four-kinds-of-functions\/","title":{"rendered":"Writing PHP Extensions \u2013 Part 3 The Four Kinds of Functions"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushCpp.js\"><\/script>\n            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/08\/writing-php-extensions-part-2-your-first-extension\/\"><strong>See Part 2 Here.<\/strong><\/a><\/p>\n<p>The PHP-CPP library defines four kinds of functions which your extension can have, based on the arguments that can be passed and the values that can be returned. All the public functions that you set up as part of your extension have to fall into one of these four kinds:<\/p>\n<ol>\n<li>Function which takes no arguments and returns no value.<\/li>\n<li>Function which takes in arguments and returns no value.<\/li>\n<li>Function which takes in no arguments and returns a value.<\/li>\n<li>Function which takes in arguments and returns a value.<\/li>\n<\/ol>\n<p>The function signatures of the above types are defined as below:<\/p>\n<ol>\n<li><em>void function();<\/em><\/li>\n<li><em>void function(Php::Parameters &amp;params);<\/em><\/li>\n<li><em>Php::Value function();<\/em><\/li>\n<li><em>Php::Value function(Php::Parameters &amp;params);<\/em><\/li>\n<\/ol>\n<p>As you can see there are only two types of data which are used to interact with the functions.<\/p>\n<h4>Php::Value and Php::Parameters<\/h4>\n<p>Php::Value is a class defined in PHP-CPP which has a flexible loose data-type , very similar to a php variable. So it can store numbers, strings, arrays and objects. Php::Value is used to return value from functions.<\/p>\n<p>Php::Parameters is also a class defined in PHP-CPP which is basically a dynamic array or a vector which contains a list of all the parameters passed into a function. This means that using a single Php::Parameters argument type, a function can take in a variable number of arguments of different data types. As the name suggests, Php::Parameters are used to pass data into functions.<\/p>\n<h4>CODE<\/h4>\n<p>The code below shows the four kinds of functions being used in the helloworld extension. As you can see the integration part is very simple &#8211; you write the function and then use the<em> extension.add <\/em>method to make it a callable function from PHP code.<\/p>\n<p>&nbsp;<\/p>\n<h4><pre class=\"brush: cpp\">#include &lt;phpcpp.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\n\r\n\/**\r\n * Function which takes no arguments and returns no values\r\n *\r\n *\/\r\nvoid sayHello() {\r\n\tstd::cout &lt;&lt; &quot;Hello World \\t This is my first PHP extension&quot; &lt;&lt; std::endl;\r\n\r\n}\r\n\r\n\/**\r\n * Function which takes in arguments and returns no value\r\n * \r\n *\/\r\n void sayHelloWithInputs(Php::Parameters &amp;params) {\r\n\t\r\n\tstd::string arg1=params[0];\r\n\r\n\tstd::cout &lt;&lt; &quot;Hello World \\t&quot; &lt;&lt; arg1 &lt;&lt; std::endl;\r\n\r\n\r\n }\r\n\r\n\/**\r\n * Function which takes no arguments and returns a value\r\n *\r\n *\/\r\n Php::Value sayHelloWithReturnValue() {\r\n\t\r\n\treturn &quot;Hello World. This is a return value.\\n&quot;;\r\n }\r\n\r\n \/**\r\n  * Function which takes in arguments and returns a value\r\n  *\r\n  *\/\r\n Php::Value sayHelloWithArgumentsAndReturnValue(Php::Parameters &amp;params) {\r\n\t\r\n\tstd::string arg1 = params[0];\r\n\tstd::string retval = &quot;Hello World \\t with argument:&quot; + arg1 + &quot;\\n&quot;;\r\n\r\n\treturn retval;\r\n } \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        extension.add(&quot;sayHello&quot;, sayHello);\r\n\t\textension.add(&quot;sayHelloWithInputs&quot;, sayHelloWithInputs);\r\n\t\textension.add(&quot;sayHelloWithReturnValue&quot;, sayHelloWithReturnValue);\r\n\t\textension.add(&quot;sayHelloWithArgumentsAndReturnValue&quot;, sayHelloWithArgumentsAndReturnValue);\r\n\r\n        \/\/ return the extension\r\n        return extension;\r\n    }\r\n}\r\n<\/pre><\/h4>\n<p>To compile the extension run<em> make<\/em> and then <em>sudo make install.<\/em> (See part 2 for details on this).<\/p>\n<p>Create a php file with the code below and run it from the php command line eg. <em>php test.php<\/em><\/p>\n<h4>PHP TEST CODE<\/h4>\n<p><pre class=\"brush: php\">&lt;?php\r\n\r\nsayHello();\r\nsayHelloWithInputs(&quot;This is a string&quot;);\r\nsayHelloWithInputs(789*10); \r\nsayHelloWithInputs(date(&quot;Y-i-d&quot;));\r\nsayHelloWithInputs(strlen(&quot;this is a string&quot;));\r\n\r\n$arr = array();\r\nfor($i = 0; $i&lt; 10; $i++) {\r\n\t$arr[$i] = $i;\r\n}\r\nsayHelloWithInputs(implode(&quot;,&quot;, $arr));\r\n\r\necho sayHelloWithReturnValue();\r\n\r\necho sayHelloWithArgumentsAndReturnValue(&quot;my parameter&quot;);\r\n\r\n?&gt;\r\n<\/pre><\/p>\n<h4>EXPLANATION<\/h4>\n<p>In the functions , we are assuming that the arguments passed are all strings or can be converted into strings. So that is why\u00a0 sayHelloWithInputs() works with all kinds of inputs: integers, arrays, strings.<\/p>\n<p>If instead of <em>sayHelloWithInputs(implode(&#8220;,&#8221;, $arr)); <\/em> you write <em>sayHelloWithInputs($arr) <\/em> it will output the string &#8220;Array&#8221; since that is the string representation of an Array object.<\/p>\n<p>The two functions <em>sayHello()<\/em> and <em>sayHelloWithInputs() <\/em> are defined as functions which will not return any values, but yet you can see the output on the display. That is because in these two functions , we are using the c++ function std::cout which dislays text to the default output device. It is much like using <em>var_dump()<\/em> in PHP which generates output.<\/p>\n<p>So if you assign <em>sayHello()<\/em> to a variable like <em>$x = sayHello()<\/em> then $x will contain the display output from the function.<\/p>\n<p><em>sayHelloWithReturnValue()<\/em> does not generate any output in its function code but it returns a value. So if you call the function as <em>sayHelloWithReturnValue();<\/em> in php it will not generate any display.<\/p>\n<p><em>$x = sayHelloWithReturnValue();<\/em> will assign the return value to $x and then you can use $x as required.<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<h4>\u00a0<a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/10\/writing-php-extensions-part-4-function-arguments\/\">Next: Part 4 Function Arguments<\/a><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>See Part 2 Here. The PHP-CPP library defines four kinds of functions which your extension can have, based on the arguments that can be passed <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/09\/writing-php-extensions-part-3-the-four-kinds-of-functions\/\" title=\"Writing PHP Extensions \u2013 Part 3 The Four Kinds of Functions\">[&#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,298],"tags":[],"class_list":["post-2259","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp","category-cc","category-linux"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2259","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=2259"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2259\/revisions"}],"predecessor-version":[{"id":2278,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2259\/revisions\/2278"}],"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=2259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}