{"id":14932,"date":"2020-06-14T09:18:18","date_gmt":"2020-06-14T09:18:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=14932"},"modified":"2021-11-05T06:45:21","modified_gmt":"2021-11-05T06:45:21","slug":"autoloading-classes-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/autoloading-classes-in-php\/","title":{"rendered":"Autoloading Classes in PHP"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">1. What are Autoloading Classes?<\/p>\n\n\n\n<p>Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"388\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/autoload.png\" alt=\"autoloading a class in php\" class=\"wp-image-14933\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/autoload.png 858w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/autoload-300x136.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/autoload-768x347.png 768w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><figcaption>autoloading a class in php<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">2. How to use Autoload Classes <\/p>\n\n\n\n<p>In PHP 5, this is no longer necessary. The&nbsp;<a href=\"https:\/\/www.php.net\/manual\/en\/function.spl-autoload-register.php\" target=\"_blank\" rel=\"noopener\">spl_autoload_register()<\/a>&nbsp;function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"192\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/spl-register.jpg\" alt=\"\" class=\"wp-image-14934\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/spl-register.jpg 561w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/spl-register-300x103.jpg 300w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption>spl_autoload_register()<\/figcaption><\/figure>\n\n\n\n<p><strong>Example #1 Autoload example<\/strong><\/p>\n\n\n\n<p>This example attempts to load the classes&nbsp;<em><strong>MyClass1<\/strong><\/em>&nbsp;and&nbsp;<em><strong>MyClass2<\/strong><\/em>&nbsp;from the files&nbsp;<var><strong>MyClass1.php<\/strong><\/var>&nbsp;and&nbsp;<var><strong>MyClass2.php<\/strong><\/var>&nbsp;respectively.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/93c4fe631b1b1331a28cce5fc70ab127.js\"><\/script>\n\n\n\n<p><strong>Example #2 Autoload other example<\/strong><\/p>\n\n\n\n<p>This example attempts to load the interface&nbsp;<strong><em>ITest<\/em>.<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/93c4fe631b1b1331a28cce5fc70ab127.js\"><\/script>\n\n\n\n<p><strong>Example #3 Autoloading with exception handling for 5.3.0+<\/strong><\/p>\n\n\n\n<p>This example throws an exception and demonstrates the try\/catch block.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/pradeeprjth\/93c4fe631b1b1331a28cce5fc70ab127.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_57725\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_92050\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_57725\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>1. What are Autoloading Classes? Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class). 2. How to use Autoload Classes In PHP 5, this is no&#8230;<\/p>\n","protected":false},"author":14,"featured_media":14935,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[1,5150],"tags":[],"class_list":["post-14932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","category-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=14932"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14932\/revisions"}],"predecessor-version":[{"id":24765,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/14932\/revisions\/24765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/14935"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=14932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=14932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=14932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}