{"id":1455,"date":"2018-01-19T19:22:51","date_gmt":"2018-01-19T19:22:51","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/pear-primer-page-2\/"},"modified":"2018-01-19T19:24:48","modified_gmt":"2018-01-19T19:24:48","slug":"pear-primer-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/pear-primer-page-2\/","title":{"rendered":"PEAR Primer Page 2"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Joe Stump<\/div>\n<div class=\"\">on December 26, 2002<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>The PEAR Class<\/h2>\n<div class=\"articlePara\">\nThe base PEAR class is fairly abstracted and shouldn&#8217;t be used on its own, however, it is a great class to<br \/>\nbuild your classes off of. It&#8217;s major feature is that it imitates destructors. With PHP5 on its way this will<br \/>\nbe void, since PHP5 will support destructors natively, I believe. The class file for PEAR also includes the<br \/>\nPEAR_Error class, which we will talk about in more detail at a later time. First let&#8217;s discuss basing your<br \/>\nclasses on PEAR. We are going to start by creating our own specific base class and then extending that to a<br \/>\nuser class to be used on a site. Remember to document your classes using <a href=\"http:\/\/www.phpdoc.de\/\" target=\"_blank\">PHPDoc<\/a>!<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>\u00a0\u00a0<\/span><span style=\"color: #007700\">require_once(<\/span><span style=\"color: #DD0000\">'PEAR.php'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0require_once(<\/span><span style=\"color: #DD0000\">'DB.php'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0require_once(<\/span><span style=\"color: #DD0000\">'Log.php'<\/span><span style=\"color: #007700\">);<\/p>\n<p>\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0*\u00a0Default\u00a0PEAR\u00a0DSN<br \/>\n<br \/>\u00a0\u00a0*\u00a0<br \/>\n<br \/>\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;<br \/>\n<br \/>\u00a0\u00a0*\u00a0@global\u00a0string\u00a0BASE_PEAR_DSN<br \/>\n<br \/>\u00a0\u00a0*\u00a0@access\u00a0public<br \/>\n<br \/>\u00a0\u00a0*\u00a0@see\u00a0Base::Base(),\u00a0Base::$db<br \/>\n<br \/>\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0<\/span><span style=\"color: #0000BB\">define<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'BASE_PEAR_DSN'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'mysql:\/\/root:@localhost\/base'<\/span><span style=\"color: #007700\">);<\/p>\n<p>\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0*\u00a0Base\u00a0Class<br \/>\n<br \/>\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0*\u00a0Our\u00a0base\u00a0class\u00a0will\u00a0hold\u00a0only\u00a0the\u00a0basic\u00a0necessities\u00a0that\u00a0all<br \/>\n<br \/>\u00a0\u00a0*\u00a0of\u00a0our\u00a0child\u00a0classes\u00a0will\u00a0need.\u00a0Mainly\u00a0DB\u00a0connectivity\u00a0and<br \/>\n<br \/>\u00a0\u00a0*\u00a0the\u00a0ability\u00a0to\u00a0log\u00a0errors.<br \/>\n<br \/>\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;<br \/>\n<br \/>\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0<\/span><span style=\"color: #007700\">class\u00a0<\/span><span style=\"color: #0000BB\">Base\u00a0<\/span><span style=\"color: #007700\">extends\u00a0<\/span><span style=\"color: #0000BB\">PEAR<br \/>\n<br \/>\u00a0\u00a0<\/span><span style=\"color: #007700\">{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0DB\u00a0Class<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@access\u00a0public<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #007700\">var\u00a0<\/span><span style=\"color: #0000BB\">$db<\/span><span style=\"color: #007700\">;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0Log\u00a0Class<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@access\u00a0public<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #007700\">var\u00a0<\/span><span style=\"color: #0000BB\">$log<\/span><span style=\"color: #007700\">;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0Base\u00a0Contstructor<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0Connect\u00a0to\u00a0the\u00a0DB\u00a0and\u00a0create\u00a0our\u00a0Log,\u00a0which\u00a0can\u00a0then\u00a0be<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0used\u00a0by\u00a0all\u00a0children\u00a0classes.<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;\u00a0<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@acces\u00a0public<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@return\u00a0void<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #007700\">function\u00a0<\/span><span style=\"color: #0000BB\">Base<\/span><span style=\"color: #007700\">()<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">PEAR<\/span><span style=\"color: #007700\">();<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(<\/span><span style=\"color: #0000BB\">get_class<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">)\u00a0==\u00a0<\/span><span style=\"color: #DD0000\">'base'<\/span><span style=\"color: #007700\">)<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this\u00a0<\/span><span style=\"color: #007700\">=\u00a0new\u00a0<\/span><span style=\"color: #0000BB\">PEAR_Error<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Base\u00a0is\u00a0an\u00a0abstracted\u00a0class!'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db\u00a0<\/span><span style=\"color: #007700\">=&amp;\u00a0<\/span><span style=\"color: #0000BB\">DB<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">connect<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">BASE_PEAR_DSN<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(<\/span><span style=\"color: #0000BB\">DB<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">isError<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db<\/span><span style=\"color: #007700\">))<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this\u00a0<\/span><span style=\"color: #007700\">=\u00a0new\u00a0<\/span><span style=\"color: #0000BB\">PEAR_Error<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">());<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">setFetchMode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">DB_FETCHMODE_ASSOC<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">log\u00a0<\/span><span style=\"color: #007700\">=&amp;\u00a0<\/span><span style=\"color: #0000BB\">Log<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">factory<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'syslog'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'Base'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/**<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0Base\u00a0Destructor<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0Just\u00a0add\u00a0a\u00a0'_'\u00a0to\u00a0your\u00a0class's\u00a0name\u00a0and\u00a0voila!\u00a0you\u00a0have\u00a0a\u00a0PEAR<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0controlled\u00a0destructor!<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@author\u00a0Joe\u00a0Stump\u00a0&lt;joe@joestump.net&gt;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@access\u00a0public<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\u00a0@return\u00a0void<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0*\/<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #007700\">function\u00a0<\/span><span style=\"color: #0000BB\">_Base<\/span><span style=\"color: #007700\">()<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(!<\/span><span style=\"color: #0000BB\">DB<\/span><span style=\"color: #007700\">::<\/span><span style=\"color: #0000BB\">isError<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db<\/span><span style=\"color: #007700\">))<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">db<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">disconnect<\/span><span style=\"color: #007700\">();<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/>\u00a0\u00a0}<\/p>\n<p><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nThere are a few things that you will notice that are quite a bit different from your average PHP class. The first is<br \/>\nit&#8217;s documented! No, just kidding, we all document our code. Jokes aside you&#8217;ll notice that all we really needed<br \/>\nto do to make our class a true PEAR class is extend it from PEAR and make sure we had a properly named destructor.<\/div>\n<div class=\"articlePara\">\nThere is a side note on destructors. When you create an instance of any PEAR based class you <b>MUST<\/b><br \/>\nassign by reference, meaning =&amp; and not just =. If you do not assign by reference then your destructors will<br \/>\n<b>NOT<\/b> run!<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"stump20021223.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump20021223.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">2<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump200212239ba9.html?page=3\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump20021223fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump20021223af4d.html?page=5\">5<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump20021223c575.html?page=6\">6<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"stump20021223235c.html?page=7\">7<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"stump200212239ba9.html?page=3\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PEAR stands for &#8220;PHP Extension and Application Repository&#8221; and has been slowly building itself over the last few years. There are classes for<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1455","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1455"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1455\/revisions"}],"predecessor-version":[{"id":3310,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1455\/revisions\/3310"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}