{"id":1687,"date":"2017-06-21T14:15:08","date_gmt":"2017-06-21T18:15:08","guid":{"rendered":"http:\/\/www.thatsoftwareguy.com\/blog\/?p=1687"},"modified":"2018-09-01T17:39:42","modified_gmt":"2018-09-01T21:39:42","slug":"including-a-file-in-a-php-class","status":"publish","type":"post","link":"https:\/\/www.thatsoftwareguy.com\/blog\/2017\/06\/21\/including-a-file-in-a-php-class\/","title":{"rendered":"Including a file in a PHP class"},"content":{"rendered":"<p>I was trying to figure out how I could manage the premium logic for my freemium <a href=\"https:\/\/www.thatsoftwareguy.com\/opencart_better_together.html\">Better Together for OpenCart<\/a> module. \u00a0You can&#8217;t just <code>require<\/code>\u00a0a block of code in the middle of a PHP Class:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code><span class=\"kwd\">class<\/span> <span class=\"typ\">SomeClass<\/span> <span class=\"pun\">{<\/span><span class=\"pln\">  \r\n  \/\/ NOT ALLOWED! \r\n  premium_file <\/span><span class=\"pun\">=<\/span> <span class=\"str\">\"premium.php\"<\/span><span class=\"pun\">;<\/span>\r\n  <span class=\"kwd\">if<\/span> <span class=\"pun\">(<\/span><span class=\"pln\">file_exists<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$premium_file<\/span><span class=\"pun\">))<\/span> <span class=\"pun\">{<\/span>\r\n    <span class=\"kwd\">require<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$premium_file<\/span><span class=\"pun\">);<\/span>\r\n  <span class=\"pun\">}<\/span><\/code><\/pre>\n<p>So I cooked up another method that I documented in this StackOverflow post about <a href=\"https:\/\/stackoverflow.com\/a\/44681321\/319741\">how to include code into a PHP class<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to figure out how I could manage the premium logic for my freemium Better Together for OpenCart module. \u00a0You can&#8217;t just require\u00a0a block of code in the middle of a PHP Class: class SomeClass { \/\/ NOT ALLOWED! premium_file = &#8220;premium.php&#8221;; if (file_exists($premium_file)) { require($premium_file); } So I cooked up another method &hellip; <a href=\"https:\/\/www.thatsoftwareguy.com\/blog\/2017\/06\/21\/including-a-file-in-a-php-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Including a file in a PHP class<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"class_list":["post-1687","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/posts\/1687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/comments?post=1687"}],"version-history":[{"count":1,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/posts\/1687\/revisions"}],"predecessor-version":[{"id":1688,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/posts\/1687\/revisions\/1688"}],"wp:attachment":[{"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/media?parent=1687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/categories?post=1687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thatsoftwareguy.com\/blog\/wp-json\/wp\/v2\/tags?post=1687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}