{"id":1370,"date":"2018-01-19T19:22:45","date_gmt":"2018-01-19T19:22:45","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-and-classes\/"},"modified":"2018-01-19T19:24:44","modified_gmt":"2018-01-19T19:24:44","slug":"php-and-classes","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-and-classes\/","title":{"rendered":"PHP and Classes"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Rod Kreisler<\/div>\n<div class=\"\">on July 30, 2000<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nThe hardest concept I&#8217;ve tried to understand since beginning to use PHP<br \/>\nwas that of classes.  I&#8217;d never used a database engine but learning to<br \/>\nuse MySQL, at least for the more basic functions, was a breeze.  Having<br \/>\nnever used OOP before, classes were novel as well, but understanding the<br \/>\ntheory and why it was useful escaped me.  I knew they must be powerful<br \/>\nas &#8220;everything&#8221; is programmed using OOP, but for the life of me,<br \/>\nalthough I thought I understood the mechanics, I couldn&#8217;t see the<br \/>\nusefulness.  Then, just a few days ago, while trying to figure out how<br \/>\nto do something with regular functions it hit me just how doing it with<br \/>\nobjects would make my job much simpler!  I&#8217;m going to try to explain<br \/>\nabout them in plain English and hopefully help others like myself.<\/div>\n<div class=\"articlePara\">\nClasses are nothing more than a collection of variables and functions<br \/>\nacting on those variables.  They provide a means of thinking about<br \/>\nthings in real world terms.  In other words they describe an object.  An<br \/>\nobject, or instance, of a class is an actual &#8220;living, breathing&#8221;<br \/>\nstructure of that class.  Let&#8217;s say we want to describe a bicycle.  A<br \/>\nproper class of a bicycle might have the variables <code class=\"example\">$pedals, $chain,<br \/>\n$front wheel, $rear wheel, $brakes<\/code>, and <code class=\"example\">$handle_bars<\/code>. Functions of the<br \/>\nbicycle would include <code class=\"example\">Stop(), Accelerate(), Coast(), TurnLeft()<\/code> and<br \/>\n<code class=\"example\">TurnRight().<\/code>  You can think of your script as the entity operating that<br \/>\nbike.  The function <code class=\"example\">Accelerate()<\/code> could be passed an argument such as<br \/>\n<code class=\"example\">$Braking_Force<\/code> and use that information along with the defined instance<br \/>\nvariables (probably <code class=\"example\">$brakes<\/code> and <code class=\"example\">$wheels<\/code>)<br \/>\nand output some result back to your script.<\/div>\n<div class=\"articlePara\">\nInteresting, but couldn&#8217;t all this be accomplished using regular<br \/>\nvariables and functions?  Yes it could, and if you only had one bike in<br \/>\nyour script it probably wouldn&#8217;t make much sense to define a class just<br \/>\nfor it.  But what if you needed several bicycles?  It could become quite<br \/>\ncomplex keeping track of all those variables and making sure you pass<br \/>\nthe correct variables to the different functions, and using objects cuts<br \/>\ndown on the number of variables you need to pass because the function<br \/>\nautomatically has available to it all the variables describing the<br \/>\nobject the function is acting upon.  Also, class definitions are easily<br \/>\nincluded in different scripts and you&#8217;ll be assured that a bicycle works<br \/>\nthe same way in each script!  <\/div>\n<div class=\"articlePara\">\nLet&#8217;s create a class that I actually use on almost every page on my site<br \/>\nand you might find useful, too.<\/div>\n<div class=\"articlePara\">\nI don&#8217;t know about you, but when I&#8217;m writing a dynamic web page I hate<br \/>\nto have to stop thinking about the logical flow to worry about properly<br \/>\nformatting my HTML. As a consequence of this, I often end up with not so<br \/>\nattractive pages because I don&#8217;t want to worry about font faces and<br \/>\nsizes, or background and text colors.  The solution:  using a PHP class<br \/>\nto set HTML output attributes with functions to format the text!<\/div>\n<div class=\"articlePara\">\nI call the class <b>&#8220;Style&#8221;.<\/b>  It contains the following variables that set<br \/>\nimportant HTML attributes for formatting the output:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<\/p>\n<p><\/span><span style=\"color: #007700\">class\u00a0<\/span><span style=\"color: #0000BB\">Style\u00a0<\/span><span style=\"color: #007700\">{<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$text<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$alink<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$vlink<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$link<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$bgcol<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$face<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$size<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$align<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0var\u00a0<\/span><span style=\"color: #0000BB\">$valign<\/span><span style=\"color: #007700\">;<\/p>\n<p>}<\/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\">\nI&#8217;m sure you&#8217;re familiar with HTML so the variables should be self-<br \/>\nexplanatory.  Next I created a function for Style called Style:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<\/p>\n<p><\/span><span style=\"color: #007700\">class\u00a0<\/span><span style=\"color: #0000BB\">Style\u00a0<\/span><span style=\"color: #007700\">{<\/p>\n<p>function\u00a0<\/span><span style=\"color: #0000BB\">Style\u00a0<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$text<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"#000000\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$alink<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"#AA00AA\"<\/span><span style=\"color: #007700\">,<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$vlink<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"#AA00AA\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$link<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"#3333FF\"<\/span><span style=\"color: #007700\">,<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$bgcol<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"#999999\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$face<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"Book\u00a0Antiqua\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$size<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">3<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$align<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"CENTER\"<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$valign<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">\"TOP\"<\/span><span style=\"color: #007700\">)\u00a0{<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">text<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$text<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">alink<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$alink<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">vlink<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$vlink<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">link<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$link<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bgcol<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$bgcol<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">face<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$face<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">size<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$size<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">align<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$align<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$this<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">valign<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$valign<\/span><span style=\"color: #007700\">;<\/p>\n<p>}<\/p>\n<p>}<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/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=\"rod199906014658.html?page=2\">2<\/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=\"rod199906019ba9.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=\"rod19990601fdb0.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=\"rod19990601af4d.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=\"rod19990601c575.html?page=6\">6<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"rod199906014658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The hardest concept I&#8217;ve tried to understand since beginning to use PHP was that of classes. I&#8217;d never used a database engine but learning to use<\/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-1370","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1370","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=1370"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1370\/revisions"}],"predecessor-version":[{"id":3245,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1370\/revisions\/3245"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}