{"id":12428,"date":"2025-01-24T09:00:00","date_gmt":"2025-01-24T09:00:00","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=12428"},"modified":"2025-01-22T11:23:33","modified_gmt":"2025-01-22T11:23:33","slug":"how-to-disable-a-class-on-a-staging-server-for-testing-purposes-php","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/how-to-disable-a-class-on-a-staging-server-for-testing-purposes-php\/","title":{"rendered":"How to &#8220;Disable&#8221; a PHP Class on a Staging Server for Testing Purposes"},"content":{"rendered":"\n<p>One of our client&#8217;s sites integrates with a third party CRM and we ran into the issue of staging site edits being pushed to it which wasn&#8217;t ideal; we&#8217;d do all sorts of testing on staging resulting in test data ending up in the live CRM account.<\/p>\n\n\n\n<p>An interesting way around this, with minimal code changes, is to create a new class which allows the original class to do &#8220;nothing&#8221; and crucially doesn&#8217;t produce an error when calling methods. Let&#8217;s take a look at the code:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>class ClassFactory{\n\n    public static function create(){\n\n        if(strpos($_SERVER[&#39;SERVER_NAME&#39;], &quot;staging.mysite.com&quot;) !== false){\n            return new NullClass();  \/\/ Return null version on staging\n        }\n\n        return new OriginalCRMClass();  \/\/ Return real class\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>The above class will check the domain of the site it&#8217;s currently on and, if the staging version, will return NullClass() as defined below, and if not on the staging site will return the original CRM class we created. <\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/\/ Null class that overrides all methods\nclass NullClass{\n\n    \/\/ Magic method to catch any method calls and do nothing\n    public function __call($name, $arguments){\n\n        \/\/ Do nothing, no matter which method is called\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>Now we can initiate the class (<code>$class = ClassFactory::create()<\/code>) and use like normal (e.g. <code>$class->updateContact($user)<\/code>) which will work as it did previously on the live site but the CRM code won&#8217;t run on the staging site, &#8220;failing&#8221; gracefully without error.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of our client&#8217;s sites integrates with a third party CRM and we ran into the issue of staging site edits being pushed to it which wasn&#8217;t ideal; we&#8217;d do all sorts of testing on staging resulting in test data ending up in the live CRM account. An interesting way around this, with minimal code [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":12439,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-12428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=12428"}],"version-history":[{"count":8,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12428\/revisions"}],"predecessor-version":[{"id":12440,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12428\/revisions\/12440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/12439"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=12428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=12428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}