{"id":502,"date":"2011-03-27T05:15:48","date_gmt":"2011-03-27T12:15:48","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=502"},"modified":"2011-03-27T05:15:48","modified_gmt":"2011-03-27T12:15:48","slug":"a-very-efficient-way-of-instantiating-a-class-dynamically-in-php","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2011\/03\/27\/a-very-efficient-way-of-instantiating-a-class-dynamically-in-php\/","title":{"rendered":"A very efficient way of instantiating a class dynamically in PHP"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>Since PHP is a loosely typed language, it provides great flexibility in creating variables and objects at runtime without having to specifically declare them first. It is well known that $i=10, $i=&#8221;test&#8221;, $i = date() will all work without creating any syntax or compile errors.<\/p>\n<p>A lesser used concept is that this feature can be used to create an object at runtime without having to check for the object type at first. Eg.if you have had 3 classes and only one of them had to be created depending on a condition the following would be the sample code:<\/p>\n<p><pre class=\"brush: php\">&lt;?php\r\n\terror_reporting(E_ALL);\r\n\r\n\tclass Generic {\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;generic&quot;);\r\n\t\t}\r\n\t}\r\n\tclass A extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;A&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tclass B extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo (&quot;B&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tclass C extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;C&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\r\n\t$test = &quot;B&quot;;\r\n\tif ($test ==&quot;B&quot;)\r\n\t\t$x = new B();\r\n\telse if ($test == &quot;A&quot;\r\n\t\t$x = new A();\r\n    else if ($test == &quot;C&quot;)\r\n\t    $x = new C();\r\n\t$x-&gt;xprint();\r\n\techo(&quot;end&quot;);\r\n?&gt;<\/pre><\/p>\n<p>Instead of doing multiple checks and then creating the relevant class, a more efficient way is to pass the classname as a string and then create it directly as shown below:<\/p>\n<p><pre class=\"brush: php\">&lt;?php\r\n\terror_reporting(E_ALL);\r\n\r\n\tclass Generic {\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;generic&quot;);\r\n\t\t}\r\n\t}\r\n\tclass A extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;A&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tclass B extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo (&quot;B&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\tclass C extends Generic{\r\n\t\tfunction xprint() {\r\n\t\t\techo(&quot;C&quot;);\r\n\t\t}\r\n\t}\r\n\r\n\r\n\t$test = &quot;B&quot;;\r\n\t$x = new $test();\r\n\t$x-&gt;xprint();\r\n\techo(&quot;end&quot;);\r\n?&gt;<\/pre><\/p>\n<p>The above code will print &#8220;B&#8221; since it creates the class B in $x. What about classes where arguments are required in the constructor? Simple call it with the arguments eg. $x = new $test(&#8220;arg1&#8221;, &#8220;arg2&#8221;);<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Since PHP is a loosely typed language, it provides great flexibility in creating variables and objects at runtime without having to specifically declare them first. <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2011\/03\/27\/a-very-efficient-way-of-instantiating-a-class-dynamically-in-php\/\" title=\"A very efficient way of instantiating a class dynamically in PHP\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[36,68,104,151,152,200,226,257],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry","category-apachephp","tag-amit","tag-class","tag-dynamic","tag-instant","tag-instantiate","tag-php","tag-runtime","tag-truelogic"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}