{"id":2268,"date":"2015-09-10T11:48:21","date_gmt":"2015-09-10T11:48:21","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2268"},"modified":"2015-09-11T12:34:47","modified_gmt":"2015-09-11T12:34:47","slug":"writing-php-extensions-part-4-function-arguments","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/10\/writing-php-extensions-part-4-function-arguments\/","title":{"rendered":"Writing PHP Extensions &#8211; Part 4 Function Arguments"},"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\/09\/writing-php-extensions-part-3-the-four-kinds-of-functions\/\"><strong>See Part 3 Here<\/strong><\/a><\/p>\n<p>Just like most other languages, you can pass arguments to your extension functions either by value or by reference. By default, all arguments are assumed to be passed by value. We introduce two more PHP-CPP classes now:<\/p>\n<p>&nbsp;<\/p>\n<h4>Php::ByVal and Php::ByRef<\/h4>\n<p>A ByVal argument passed to a function only has local scope inside the function. Whatever manipulation is done to the data within the function does not affect its value outside the function from where it was called because the compiler makes a copy of the data and sends it to the function rather than send the actual data.<\/p>\n<p>A ByRef argument has scope outside the function even after the function has finished executing. That means if the argument data is changed within the function, when the control returns to the calling code, the data will contain the changed value instead of the value which was passed into the function.<\/p>\n<p>The compiler sends the original data into the function for a ByRef argument so its new value is available after the function has finished executing.<\/p>\n<p>Function arguments can be specified in the extension.add() method as a third argument<\/p>\n<pre>\u00a0extensions.add(\"myfunction\", myfunction, {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Php::ByVal(\"rate\", Php::Type::Numeric),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Php::ByVal(\"quantity\", Php::Type::Numeric)\r\n})\r\n<\/pre>\n<p>In the above code we have specified that myfunction() will expect two arguments, both of type numeric. The syntax of declaring a ByVal is :<\/p>\n<pre><code class=\"language-cpp hljs\">ByVal(<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-keyword\">char<\/span> *name, Php::Type type, <span class=\"hljs-keyword\">bool<\/span> required = <span class=\"hljs-keyword\">true<\/span>);<\/code><\/pre>\n<p>The first argument is the name of the argument, the second defines the type and the third argument defines whether the argument is required or is optional. By default , all arguments are required.<\/p>\n<p>We have a list of predefined Php::Type classes:<\/p>\n<pre><code class=\"hljs cpp\">Php::Type::Null\r\nPhp::Type::Numeric\r\nPhp::Type::Float\r\nPhp::Type::Bool\r\nPhp::Type::Array\r\nPhp::Type::Object\r\nPhp::Type::String\r\nPhp::Type::Resource\r\nPhp::Type::Constant\r\nPhp::Type::ConstantArray\r\nPhp::Type::Callable\r\n<\/code><\/pre>\n<p>Note that Php::ByRef classes are also declared the same way. Using the above example:<\/p>\n<pre>\u00a0extensions.add(\"myfunction\", myfunction, {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Php::ByRef(\"rate\", Php::Type::Numeric),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Php::ByRef(\"quantity\", Php::Type::Numeric)\r\n})\r\n\r\n<\/pre>\n<h4>Example Code<\/h4>\n<p>Lets add two functions to our helloworld extension. <em>fixedArguments() <\/em> will take in two numbers and return their sum. <em>fixedArgumentsByRef() <\/em> will do the same thing but return the sum back in the first argument.<br \/>\n<pre class=\"brush: cpp\">#include &lt;phpcpp.h&gt;\r\n#include &lt;iostream&gt;\r\n\r\n\r\n\r\n \/**\r\n  * Fixed arguments type passed by value\r\n  *\r\n  *\/\r\n Php::Value fixedArguments(Php::Parameters &amp;params) {\r\n\t\r\n\tint arg1 = params[0];\r\n\tint arg2 = params[1];\r\n\r\n\treturn (arg1 + arg2);\r\n }\r\n\r\n\r\n\/**\r\n * Fixed arguments type passed by ref with one of the args changed in the function\r\n * \r\n *\/\r\n void fixedArgumentsByRef(Php::Parameters &amp;params) {\r\n \r\n\tint arg1 = params[0];\r\n\tint arg2 = params[1];\r\n\r\n\targ1 = arg1 + arg2;\r\n\tparams[0] = arg1;\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\t\textension.add(&quot;fixedArguments&quot;, fixedArguments, {\r\n\t\t\tPhp::ByVal(&quot;number1&quot;, Php::Type::Numeric),\r\n\t\t\tPhp::ByVal(&quot;number2&quot;, Php::Type::Numeric)\r\n\t\t});\r\n\r\n\t\textension.add(&quot;fixedArgumentsByRef&quot;, fixedArgumentsByRef, {\r\n\t\t\tPhp::ByRef(&quot;number1&quot;, Php::Type::Numeric),\r\n\t\t\tPhp::ByRef(&quot;number2&quot;, Php::Type::Numeric)\r\n\t\t});\r\n\r\n        \/\/ return the extension\r\n        return extension;\r\n    }\r\n}\r\n<\/pre><\/p>\n<p>Run <em>make <\/em>and then <em>sudo make install <\/em>to update the extension<\/p>\n<p>The php test code is given below:<br \/>\n<pre class=\"brush: php\">&lt;?php\r\n\r\n$total = fixedArguments(1,2);\r\necho $total . &quot;\\n&quot;;\r\n\r\n$a = 1;\r\n$b = 2;\r\necho(&quot;a=&quot; . $a . &quot;,b=&quot;. $b . &quot;\\n&quot;);\r\nfixedArgumentsByRef($a, $b);\r\necho(&quot;After function: a=&quot; . $a. &quot;,b=&quot;. $b . &quot;\\n&quot;);\r\n\r\n?&gt;<\/pre><\/p>\n<p>&nbsp;<\/p>\n<p>Both the functions assume that the arguments passed are of the type int. What happens if you pass non-integers as arguments? Eg. <em>fixedArguments(&#8220;xxx&#8221;, &#8220;yyy&#8221;)<\/em>. No error or exception will be thrown. Since PHP-CPP will not be able to take the values passed as integers it will use the default values of int for the parameters so it will become 0+0 = 0.<\/p>\n<h4><a href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/11\/writing-php-extensions-part-5-web-enabling-your-extension\/\">Next: Part 5 Web-enabling your extension<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>See Part 3 Here Just like most other languages, you can pass arguments to your extension functions either by value or by reference. By default, <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/10\/writing-php-extensions-part-4-function-arguments\/\" title=\"Writing PHP Extensions &#8211; Part 4 Function Arguments\">[&#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-2268","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\/2268","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=2268"}],"version-history":[{"count":9,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2268\/revisions"}],"predecessor-version":[{"id":2289,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2268\/revisions\/2289"}],"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=2268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}