{"id":304,"date":"2014-08-11T21:51:16","date_gmt":"2014-08-12T05:51:16","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=304"},"modified":"2014-08-11T22:23:27","modified_gmt":"2014-08-12T06:23:27","slug":"__autoload-function-in-php","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=304","title":{"rendered":"__autoload function in PHP"},"content":{"rendered":"<p>In PHP, the __autoload function is used to simplify the job of the programmer by including classes automatically without the programmer having to add a very large number of include statements. An example will help clarify. Suppose we have the following code:<\/p>\n<p>include &#8220;class\/class.Foo.php&#8221;;<br \/>\ninclude &#8220;class\/class.AB.php&#8221;;<br \/>\ninclude &#8220;class\/class.XZ.php&#8221;;<br \/>\ninclude &#8220;class\/class.YZ.php&#8221;;<\/p>\n<p>$foo = new Foo;<br \/>\n$ab = new AB;<br \/>\n$xz = new XZ;<br \/>\n$yz = new YZ;<\/p>\n<p>Note in the code above that we have to include each of the 4 different class files separately \u2013 because we are creating an instance of each class, we absolutely must have each class file. Of course, we are assuming that developers are defining only one class per source file \u2013 which is good practice when writing object oriented programs, even though you are allowed to have multiple classes in one source file.<\/p>\n<p>Imagine if we need to use 20 or even 30 different classes within this one file \u2013 writing out each include statement can become a huge pain. And this is exactly the problem that the PHP __autoload function solves \u2013 it allows PHP to load the classes for us automatically! So, instead of the code above, we can use the __autoload function as shown below:<\/p>\n<p>function __autoload($class_name)<br \/>\n{<br \/>\n    require_once \u201c.\/class\/class.\u201d.$class_name.\u201c.php\u201d;<br \/>\n}<\/p>\n<p>$foo = new Foo;<br \/>\n$ab = new AB;<br \/>\n$xz = new XZ;<br \/>\n$yz = new YZ;<\/p>\n<p>Autoloading works like this. You create a function called __autoload() near the start of your PHP application. Then, whenever your code tries to create a new instance of a class that PHP doesn&#8217;t know about, PHP automatically calls your __autoload() function, passing in the name of the class it&#8217;s looking for. Your function&#8217;s job is to locate and include the class file, thereby loading the class. PHP can then go ahead and create the object.<\/p>\n<p><strong>spl_autoload_register<\/strong><br \/>\nspl_autoload_register provides a way to define more than one __autoload function using spl_autoload_register. If you already have an __autoload function you will need to register that function before registering any additional functions though.<br \/>\nspl_autoload_register(&#8216;__autoload&#8217;);<br \/>\nspl_autoload_register(&#8216;my_other__autoload&#8217;);<\/p>\n<p>Additionally, spl_autoload_register accepts any &#8216;callable&#8217; type variable, meaning that you can use a method from a class as an autoload function as well.<br \/>\n\/\/for a static method<br \/>\nspl_autoload_register(array(&#8216;MyAlreadyLoadedClass&#8217;, &#8216;autoloader&#8217;));<\/p>\n<p>Its recommended to use spl_autoload_register since __autoload may be deprecated in the future. <\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"72yWGpAiUU\"><p><a href=\"https:\/\/www.programmerinterview.com\/php-questions\/php-example-of-the-__autoload-function\/\">PHP: Example of the __autoload function<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;PHP: Example of the __autoload function&#8221; &#8212; Programmer and Software Interview Questions and Answers\" src=\"https:\/\/www.programmerinterview.com\/php-questions\/php-example-of-the-__autoload-function\/embed\/#?secret=72yWGpAiUU\" data-secret=\"72yWGpAiUU\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP, the __autoload function is used to simplify the job of the programmer by including classes automatically without the programmer having to add a very large number of include statements. An example will help clarify. Suppose we have the following code: include &#8220;class\/class.Foo.php&#8221;; include &#8220;class\/class.AB.php&#8221;; include &#8220;class\/class.XZ.php&#8221;; include &#8220;class\/class.YZ.php&#8221;; $foo = new Foo; $ab &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=304\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">__autoload function in PHP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":2,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":308,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/308"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}