{"id":1371,"date":"2018-01-19T19:22:45","date_gmt":"2018-01-19T19:22:45","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-and-classes-page-2\/"},"modified":"2018-01-19T19:24:44","modified_gmt":"2018-01-19T19:24:44","slug":"php-and-classes-page-2","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-and-classes-page-2\/","title":{"rendered":"PHP and Classes Page 2"},"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\">\nWhen you create a function within a class with the same name as the<br \/>\nclass that function will execute whenever you create an object of that<br \/>\nclass.  This is called a &#8216;constructor.&#8217;  It allows me to have default<br \/>\nvalues for each attribute automatically whenever I create an object:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php\u00a0$Basic\u00a0<\/span><span style=\"color: #007700\">=\u00a0new\u00a0<\/span><span style=\"color: #0000BB\">Style<\/span><span style=\"color: #007700\">;\u00a0<\/span><span style=\"color: #0000BB\">?&gt;<\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nYou define an instance of a class by giving it a name ($Basic) and<br \/>\nassigning it &#8220;=new ClassName;&#8221;.<\/div>\n<div class=\"articlePara\">\nYou could also send different values for the variables as arguments when<br \/>\ndeclaring the new Style, but if you declare a value you have to declare<br \/>\nall of them that occur to the right of the one you declare (class<br \/>\nfunctions work just like regular functions in this respect).  Meaning if<br \/>\nyou set &#8216;text&#8217; to something different than the default given in Style,<br \/>\nthen you have to declare all the variables. There is an easier way.  We<br \/>\ncan create a function that changes a single variable within the class:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php<\/p>\n<p><\/span><span style=\"color: #007700\">Function\u00a0<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$varname<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$value<\/span><span style=\"color: #007700\">)\u00a0{<\/p>\n<p>\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\">$varname<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">$value<\/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\">\nSo to change the value of particular variable of an instance we can:<\/div>\n<div class=\"articlePara\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php\u00a0$Basic<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'size'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'2'<\/span><span style=\"color: #007700\">);\u00a0<\/span><span style=\"color: #0000BB\">?&gt;<\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nYou use the &#8220;-&gt;&#8221; operator to refer to a variable or a function of an<br \/>\ninstance.  So the above tells the interpreter &#8220;Run function &#8216;Set()&#8217; of<br \/>\ninstance &#8216;$Basic&#8217;.&#8221;  It knows that &#8220;$Basic&#8221; is an instance of class<br \/>\n&#8220;Styles&#8221; because we declared it so.  Similarly we can refer to variables<br \/>\nof an instance in the same manner (e.g. $Basic-&gt;text).<\/div>\n<div class=\"articlePara\">\nLet&#8217;s create a Style for table headers that has some slightly different<br \/>\nattributes.<\/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>$Theader<\/span><span style=\"color: #007700\">=\u00a0new\u00a0<\/span><span style=\"color: #0000BB\">Style<\/span><span style=\"color: #007700\">;<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$Theader<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'text'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'#0000FF'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$Theader<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'bgcol'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'#000000'<\/span><span style=\"color: #007700\">);<\/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, that&#8217;s good enough.  Now my table header has blue text on a black<br \/>\nbackground.  I want my table body to be a slightly lighter gray than my<br \/>\nmain page, black is good for text, but maybe I&#8217;ll make the text smaller:<\/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>$Tbody<\/span><span style=\"color: #007700\">=new\u00a0<\/span><span style=\"color: #0000BB\">Style<\/span><span style=\"color: #007700\">;<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$Tbody<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'bgcol'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'#AAAAAA'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$Tbody<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">Set<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'size'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">);<\/p>\n<p><\/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=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"rod19990601.html\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"rod19990601.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=\"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=\"rod199906019ba9.html?page=3\">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-1371","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1371","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=1371"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1371\/revisions"}],"predecessor-version":[{"id":3246,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1371\/revisions\/3246"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}