{"id":45,"date":"2008-03-12T08:02:47","date_gmt":"2008-03-12T07:02:47","guid":{"rendered":"https:\/\/paul-anton.vanhandel.at\/?p=45"},"modified":"2008-03-12T08:02:47","modified_gmt":"2008-03-12T07:02:47","slug":"php-class-constants-a-diatribe","status":"publish","type":"post","link":"https:\/\/paul-anton.vanhandel.at\/?p=45","title":{"rendered":"PHP class constants &#8211; a diatribe (sort of)"},"content":{"rendered":"<p><em><strong style=\"font-size: large\">So constants are constant, no kidding, eh?<\/strong><\/em><br \/>\n<!--adsense#leaderboard--><br \/>\nBut why to hell can&#8217;t I assign variable content to it? Wouldn&#8217;t make sense, you say? Constants ARE constant?<\/p>\n<p>ASSIGN &#8211; NOT REassign, buddy!<\/p>\n<p>You won&#8217;t find a solution in this text. There are workarounds all over the net &#8211; that&#8217;s not the issue here &#8211; I simply had the urge to nag, complain an moan about that fact .<\/p>\n<p>Figure this (just making up this example for demonstration, but it&#8217;s as good as any):<br \/>\n<!--more-->You have a class <strong>SpitOutFQUserImageURL<\/strong> to get you your users profile picture&#8217;s fully qualified URL.<br \/>\n(yeah, you read right &#8211; FQ is not a new abbreviation for Fucking :))<\/p>\n<p>Right on &#8211; you&#8217;ve got your basic config file with all the well defined constants &#8211; you want to be as hardware and software agnostic as you can be.<\/p>\n<p>First you have the protocol defined in <strong>PROTOCOL<\/strong>: <em>http:\/\/<\/em><br \/>\nYou got the constant <strong>SITEBASE<\/strong> : Let it be <em>fileyourthoughts.com<\/em>.<br \/>\nYou got the subdomain defined in <strong>SUBDOMAIN<\/strong>: <em>www<\/em><br \/>\nThen there is <strong>MEDIAPATH<\/strong>: <em>mediastore<\/em><br \/>\nFinally there&#8217;s <strong>IMAGEPATH<\/strong> being: <em>userimages<\/em><\/p>\n<p>Now that class is simple enough, does what it has to do bla bla bla and then a method builds the FQ URL:<code><br \/>\npublic function spitItOut() {<br \/>\n$urlBase = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '\/' . MEDIAPATH . '\/' . IMAGEPATH . '\/' . $this-&gt;getYourFuckingImage($userId);<br \/>\nreturn $urlBase;<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Yeah yeah, we could just leave it like that and it will work.<\/p>\n<p>But I don&#8217;t like it! Why can&#8217;t I &#8211; for readability if not anything else &#8211; define a class constant USERIMAGEPATH like that:<br \/>\n<code><br \/>\nclass SpitOutFQUserImageURL {<br \/>\nconst USERIMAGEPATH = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '\/' . MEDIAPATH . '\/' . IMAGEPATH . '\/';<br \/>\n}<br \/>\n<\/code><\/p>\n<p>and have my method read like:<br \/>\n<code><br \/>\npublic function spitItOut() {<br \/>\n$userImg = self::USERIMAGEPATH . $this-&gt;getYourFuckingImage($userId);<br \/>\nreturn $userImg;<br \/>\n}<\/code><\/p>\n<p>Halt! Don&#8217;t copy that stuff above &#8211; it won&#8217;t work!<br \/>\nI wish it would!<\/p>\n<p>Why?<\/p>\n<p>If you&#8217;re working on larger projects you work with packages, that, if done right, can be re-used in other projects (or part of the same project) w\/o doing to much adjustments.<br \/>\nNow imagine this class here is part of a package that does all the image handling you need and you want to use it somewhere else &#8211; different project, different environment, <strong>DIFFERENT<\/strong> configuration.<\/p>\n<ul>\n<li>What are you going to do? Right, browse the code, find every fucking reference to the config file constants and adjust &#8211; good luck, have fun.<\/li>\n<\/ul>\n<p><em>Versus<\/em><\/p>\n<ul>\n<li>You just go to the top of your class and do the adjustments there.<\/li>\n<\/ul>\n<p>I could have used a variable and assign the constants there, you say?. Yeah, I could have in the above example &#8211; although as the code grows so do these things tend to grow with it. Get&#8217;s you one hell of a constructor with lots of &#8216;work-around&#8217; junk in it.<\/p>\n<p>But what if the method was <strong>static<\/strong>???<br \/>\n<code>class SpitOutFQUserImageURL {<br \/>\nconst USERIMAGEPATH = PROTOCOL . SUBDOMAIN . '.' . SITEBASE . '\/' . MEDIAPATH . '\/' . IMAGEPATH . '\/';<br \/>\n<\/code><\/p>\n<p><code> public static function spitItOut($userId) {<br \/>\n$userImg = self::USERIMAGEPATH . self::getYourFuckingImage($userId);<br \/>\nreturn $userImg;<br \/>\n}<br \/>\n}<\/code><br \/>\n<strong> Where would I define that varibale now?<br \/>\n<\/strong><em>(Again, that above example WON&#8217;T work! Again I wish it would)<\/em><strong><br \/>\n<\/strong><\/p>\n<p>As I said &#8211; you can work around everything but my conclusion is:<br \/>\nPHP class constant assignment <strong>sucks<\/strong>!<\/p>\n<p><em>PS: YES, I DO indent my code, but this godforsaken editor keeps outdenting it&#8230;<\/em><\/p>\n<p>PPS: This was written looking at PHP 5.2.3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So constants are constant, no kidding, eh? But why to hell can&#8217;t I assign variable content to it? Wouldn&#8217;t make sense, you say? Constants ARE constant? ASSIGN &#8211; NOT REassign, buddy! You won&#8217;t find a solution in this text. There are workarounds all over the net &#8211; that&#8217;s not the issue here &#8211; I simply <a href=\"https:\/\/paul-anton.vanhandel.at\/?p=45\"> [&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,3,11],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-devel","category-allgemein","category-english","category-php"],"_links":{"self":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45"}],"version-history":[{"count":0,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}