{"id":2295,"date":"2015-09-13T04:55:03","date_gmt":"2015-09-13T04:55:03","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2295"},"modified":"2015-09-13T04:55:03","modified_gmt":"2015-09-13T04:55:03","slug":"writing-php-extensions-part-6-adding-a-class-to-your-extension","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/13\/writing-php-extensions-part-6-adding-a-class-to-your-extension\/","title":{"rendered":"Writing PHP Extensions &#8211; Part 6 Adding a Class to your 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\/11\/writing-php-extensions-part-5-web-enabling-your-extension\/\"><strong>See Part 5 Here<\/strong><\/a><\/p>\n<p>We are now going to look at how to create a C++ class to encapsulate our extension code. This is useful if you want to group similar functions or methods in one structure.<\/p>\n<p>In the sample code below, we will setup a class to display a Yearly calendar as a HTML table.\u00a0 The helloworld extension will define a class called <em>Cal<\/em> which will have one callable method <em>getYearly(&lt;year&gt;) .\u00a0 <\/em>The year passed as argument is what will be printed. Obviously this extension is only useful when called in a web page, although it can be used in a command line php also.<\/p>\n<h4>WRITING CLASSES<\/h4>\n<p>Creating a C++ class in your extension main.cpp requires small changes to the standard C++ code .<\/p>\n<ol>\n<li>All classes must be derived from <em>Php::Base<\/em><\/li>\n<li>Any class method which will be callable from PHP should use <em>Php::Value<\/em> for returning values and <em>Php::Parameters<\/em> for passing arguments.<\/li>\n<li>Any method which are used only internally within the class need not follow any special PHP considerations.<\/li>\n<li>In the <em>*get_module()<\/em> function we need to declare our class and its public methods and then use <em>extension.add()<\/em> to add the declared class.<\/li>\n<\/ol>\n<h4>THE CALENDAR CODE<\/h4>\n<p><pre class=\"brush: cpp\">#include &lt;phpcpp.h&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;iomanip&gt;\r\n#include &lt;string&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Cal : public Php::Base {\r\n\r\n\tpublic:\r\n\t\tCal(void);\r\n\t\tPhp::Value getYearly(Php::Parameters &amp;params);\r\n\r\n\tprotected:\r\n\t\tint mYear;\r\n\t\tstring mMonths[13];\r\n\t\tstring mWeekDays [8];\r\n\t\tint mDays [13];\r\n\t\tint firstDayOfMonth(int month, int year);\r\n\r\n\r\n};\r\n\r\n\/**\r\n * Constructor - initialize data \r\n *\r\n *\/\r\nCal::Cal(void) {\r\n\t\tmMonths[0] = &quot;&quot;;\t\/\/ month starts from 1\r\n\t\tmMonths[1] = &quot;January&quot;;\r\n\t\tmMonths[2] = &quot;February&quot;;\r\n\t\tmMonths[3] = &quot;March&quot;;\r\n\t\tmMonths[4] = &quot;April&quot;;\r\n\t\tmMonths[5] = &quot;May&quot;;\r\n\t\tmMonths[6] = &quot;June&quot;;\r\n\t\tmMonths[7] = &quot;July&quot;;\r\n\t\tmMonths[8] = &quot;August&quot;;\r\n\t\tmMonths[9] = &quot;September&quot;;\r\n\t\tmMonths[10] = &quot;October&quot;;\r\n\t\tmMonths[11] = &quot;November&quot;;\r\n\t\tmMonths[12] = &quot;December&quot;;\r\n\r\n\t\tmDays[0] =0;\r\n\t\tmDays[1] = 31;\r\n\t\tmDays[2] = 28;\r\n\t\tmDays[3] = 31;\r\n\t\tmDays[4] = 30;\r\n\t\tmDays[5] = 31;\r\n\t\tmDays[6] = 30;\r\n\t\tmDays[7] = 31;\r\n\t\tmDays[8] = 31;\r\n\t\tmDays[9] = 30;\r\n\t\tmDays[10] = 31;\r\n\t\tmDays[11] = 30;\r\n\t\tmDays[12] = 31;\r\n\r\n\t\tmWeekDays[0] = &quot;&quot;;\r\n\t\tmWeekDays[1] = &quot;Sun&quot;;\r\n\t\tmWeekDays[2] = &quot;Mon&quot;;\r\n\t\tmWeekDays[3] = &quot;Tue&quot;;\r\n\t\tmWeekDays[4] = &quot;Wed&quot;;\r\n\t\tmWeekDays[5] = &quot;Thu&quot;;\r\n\t\tmWeekDays[6] = &quot;Fri&quot;;\r\n\t\tmWeekDays[7] = &quot;Sat&quot;;\r\n\r\n\r\n}\r\n\r\n\/**\r\n * Using a standard c++ algorithm to find day of week for a date\r\n * Parameters:\r\n *  month - month number\r\n *  year - year\r\n * Returns:\r\n *\tdow = day of week, sun =0, mon=1 etc.\r\n *\/\r\nint Cal::firstDayOfMonth(int month, int year) {\r\n\tint dow = 0;\r\n\tint day = 1;\r\n\r\n    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };\r\n    year -= month &lt; 3;\r\n    dow = ( year + year\/4 - year\/100 + year\/400 + t[month-1] + day) % 7;\r\n\r\n\treturn dow;\r\n}\r\n\r\n\/**\r\n * Print yearly calendar as html &lt;table&gt;\r\n * Each row will have 7 td columns for the days of the week\r\n * Parameters:\r\n *  year - year to print for\r\n *\/\r\nPhp::Value  Cal::getYearly(Php::Parameters &amp;params) {\r\n\tint year = 0;\r\n\r\n\tyear = (int) params[0];\r\n\r\n\tstring output = &quot;&quot;;\r\n\r\n\tmYear = year;\r\n\tif (mYear &lt; 1900 || mYear &gt; 2100) {\r\n\t\tstd::cout &lt;&lt; &quot;Year is &quot; &lt;&lt; mYear &lt;&lt; &quot;. Year must be between 1900 and 2100&quot; &lt;&lt; endl;\r\n\t\treturn output;\r\n\t}\r\n\r\n\t\/\/ check for leap year\r\n\tif ((mYear % 4 == 0 &amp;&amp; mYear % 100 != 0) || (mYear % 400 == 0))\r\n\t\tmDays[2] = 29;\r\n\r\n\toutput.append(&quot;&lt;table width=100%% cellpadding=2 cellspacing=2 border=1&gt;&quot;);\r\n\r\n\tfor (int i =1; i &lt;=12; i ++) {\r\n\t\t\r\n\t\toutput.append(&quot;&lt;tr valign=top&gt;&quot;);\r\n\t\toutput.append(&quot;&lt;td colspan=7 align=center&gt;&lt;b&gt;&quot;);\r\n\t\toutput.append(mMonths[i]);\r\n\t\toutput.append(&quot;, &quot;);\r\n\t\toutput.append(to_string(mYear));\r\n\t\toutput.append(&quot;&lt;\/td&gt;&lt;\/tr&gt;&quot;);\r\n\r\n\t\t\/\/print weekday labels\r\n\t\toutput.append(&quot;&lt;tr valign=top&gt;&quot;);\r\n\t\tfor (int j = 1; j &lt;= 7; j++) {\r\n\t\t\toutput.append(&quot;&lt;td&gt;&lt;font color=red&gt;&quot;);\r\n\t\t\toutput.append(mWeekDays[j]);\r\n\t\t\toutput.append(&quot;&lt;\/font&gt;&lt;\/td&gt;&quot;);\r\n\t\t}\r\n\t\toutput.append(&quot;&lt;\/tr&gt;&quot;);\r\n\r\n\t\t\/\/ print days\r\n\t\tint daysPrinted = 0;\r\n\t\toutput.append(&quot;&lt;tr valing=top&gt;&quot;);\r\n\t\tfor (int j = 1; j &lt;= mDays[i]; j++) {\r\n\t\t\t\/\/ move display head to the correct day of week for 1st\r\n\t\t\tif (j == 1) {\r\n\t\t\t\tint dow = firstDayOfMonth(i, mYear);\r\n\t\t\t\tfor (int spaces=0; spaces &lt; dow; spaces++) {\r\n\t\t\t\t\toutput.append(&quot;&lt;td&gt;&amp;nbsp;&lt;\/td&gt;&quot;);\r\n\t\t\t\t\tdaysPrinted ++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\toutput.append(&quot;&lt;td&gt;&quot;);\r\n\t\t\toutput.append(to_string(j));\r\n\t\t\toutput.append(&quot;&lt;\/td&gt;&quot;);\r\n\t\t\tdaysPrinted ++;\r\n\r\n\t\t\tif (daysPrinted % 7 == 0) {\r\n\t\t\t\toutput.append(&quot;&lt;\/tr&gt;&quot;);\r\n\t\t\t\t\/\/ start next row if more days are left\r\n\t\t\t\tif (j &lt; mDays[i])\r\n\t\t\t\t\toutput.append(&quot;&lt;tr valign=top&gt;&quot;);\r\n\t\t\t\tdaysPrinted = 0;\r\n\t\t\t}\r\n\t\t} \/\/ for (int j = 1; j &lt;= mDays[i]; j++) \r\n\r\n\r\n\t\t\/\/ close pending &lt;tr&gt;\r\n\t\tif (daysPrinted &gt; 0)\r\n\t\t\toutput.append(&quot;&lt;\/tr&gt;&quot;);\r\n\t\r\n\t} \/\/ \tfor (int i =1; i &lt;=12; i ++) \r\n\toutput.append(&quot;&lt;\/table&gt;&quot;);\t\t\r\n\r\n\treturn output;\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\r\n\t\tPhp::Class&lt;Cal&gt; cal(&quot;Cal&quot;);\r\n\t\tcal.method(&quot;getYearly&quot;, &amp;Cal::getYearly, \r\n\t\t\t\t{Php::ByVal(&quot;year&quot;, Php::Type::Numeric, true)}\r\n\t\t);\r\n\t\textension.add(std::move(cal));\r\n\r\n        \/\/ return the extension\r\n        return extension;\r\n    }\r\n}\r\n<\/pre><\/p>\n<p>Follow the normal steps to build the extension as given in the previous posts:<\/p>\n<ul>\n<li>Run <em>make<\/em><\/li>\n<li>Run <em>sudo make install<\/em><\/li>\n<li>Run <em>sudo .\/webenable<\/em><\/li>\n<\/ul>\n<p>The PHP code to test the extension is just a few lines of code:<\/p>\n<p>&nbsp;<\/p>\n<pre>$cal = new Cal();\r\n$calendar = $cal-&gt;getYearly(2015);\r\necho($calendar);\r\n\r\nThe web page output is shown below:\r\n\r\n<a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-13-102200.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2299\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-13-102200-620x583.png\" alt=\"Screenshot from 2015-09-13 10:22:00\" width=\"620\" height=\"583\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-13-102200-620x583.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-13-102200-300x282.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/Screenshot-from-2015-09-13-102200.png 657w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>See Part 5 Here We are now going to look at how to create a C++ class to encapsulate our extension code. This is useful <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/13\/writing-php-extensions-part-6-adding-a-class-to-your-extension\/\" title=\"Writing PHP Extensions &#8211; Part 6 Adding a Class to your 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,298],"tags":[],"class_list":["post-2295","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\/2295","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=2295"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2295\/revisions"}],"predecessor-version":[{"id":2300,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2295\/revisions\/2300"}],"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=2295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}