{"id":35171,"date":"2019-07-04T11:12:17","date_gmt":"2019-07-04T11:12:17","guid":{"rendered":"https:\/\/www.knowband.com\/blog\/?p=35171\/"},"modified":"2019-07-12T15:38:14","modified_gmt":"2019-07-12T06:08:14","slug":"auto-loading-in-php","status":"publish","type":"post","link":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/","title":{"rendered":"Auto Loading in PHP"},"content":{"rendered":"<p><strong>Objective:<\/strong> To learn how we can use any instance of class without including the class file.<\/p>\n<p>Generally, If we want to use the instance of any class then first we need to include the class file by any of the 4 functions:<\/p>\n<ol>\n<li>include<\/li>\n<li>include_once<\/li>\n<li>require<\/li>\n<li>require_once<\/li>\n<\/ol>\n<p>Like below,<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">\/\/ included all the classes\r\nrequire &quot;classes\/Testing1.php&quot;;\r\nrequire &quot;classes\/Testing2.php&quot;;\r\n\r\n\/\/now using the functionality of class\r\n$testing1 = new Testing1;\r\necho $testing1-&gt;a;\r\n\r\n$testing2 = new Testing2;\r\necho $testing2-&gt;a;<\/pre>\n<p>But if there are many classes which we want to use then we will have to include all files to be able to make the instance of the class. Suppose if there are hundreds of classes then we will have to include all and it will take lot of time and space. To overcome with this, we can use below code:<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">function auto_loader($class) {\r\n$file = &quot;classes\/{$class}.php&quot;;    \/\/path of all the classes\r\n\/\/this will check if file exist\r\nif (is_file($file)) {\r\n\/\/finally if file exist then it will include the file\r\ninclude $file;\r\n}\r\n}\r\n\/\/ To register auto_loader() to call this function sequentially when a new class is declared\r\nspl_autoload_register(&quot;auto_loader&quot;);\r\n\r\n\/\/now using the functionality of class\r\n$testing1 = new Testing1;\r\necho $testing1-&gt;a;\r\n\r\n$testing2 = new Testing2;\r\necho $testing2-&gt;a;<\/pre>\n<p>Using the above code, the required class will be included automatically while using the class. It will not include other classes which are not required.<br \/>\nFile Structure:<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">index.php\r\nclasses\/Testing1.php\r\nclasses\/Testing2.php<\/pre>\n<p>Related Article: <a href=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/namespace-in-php\/\">Namespace in PHP<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objective: To learn how we can use any instance of class without including the class file. Generally, If we want to use the instance of any class then first we need to include the class file by any of the 4 functions: include include_once require require_once Like below, \/\/ included all the classes require &quot;classes\/Testing1.php&quot;; [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kbui_enabled":"","footnotes":""},"categories":[4],"tags":[14403,7509,14402],"class_list":["post-35171","post","type-post","status-publish","format-standard","hentry","category-ecommerce-blog","tag-auto-loading","tag-php","tag-php-auto-loading"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Auto Loading in PHP<\/title>\n<meta name=\"description\" content=\"Read the blog to learn how we can use any instance of class without including the class file.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Auto Loading in PHP\" \/>\n<meta property=\"og:description\" content=\"Read the blog to learn how we can use any instance of class without including the class file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowband Blog | Ecommerce Modules\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Knowband\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-04T11:12:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-12T06:08:14+00:00\" \/>\n<meta name=\"author\" content=\"Akash Roshan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:site\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Akash Roshan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/\"},\"author\":{\"name\":\"Akash Roshan\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/b6c3192deed20653bb1cbe0ab61c95e8\"},\"headline\":\"Auto Loading in PHP\",\"datePublished\":\"2019-07-04T11:12:17+00:00\",\"dateModified\":\"2019-07-12T06:08:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/\"},\"wordCount\":146,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"keywords\":[\"auto loading\",\"PHP\",\"PHP auto loading\"],\"articleSection\":[\"Ecommerce\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/\",\"name\":\"Auto Loading in PHP\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\"},\"datePublished\":\"2019-07-04T11:12:17+00:00\",\"dateModified\":\"2019-07-12T06:08:14+00:00\",\"description\":\"Read the blog to learn how we can use any instance of class without including the class file.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/ecommerce-blog\\\/auto-loading-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto Loading in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"description\":\"Magento, Prestashop, Opencart, Shopify\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"width\":153,\"height\":61,\"caption\":\"Knowband Blog | Ecommerce Modules\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Knowband\",\"https:\\\/\\\/x.com\\\/KnowbandPlugin\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/b6c3192deed20653bb1cbe0ab61c95e8\",\"name\":\"Akash Roshan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"caption\":\"Akash Roshan\"},\"description\":\"Akash is a PHP developer and wants to share his knowledge through Blogs. He is currently growing in the IT industry while fulfilling his own goals.\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/author\\\/aroshan\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Auto Loading in PHP","description":"Read the blog to learn how we can use any instance of class without including the class file.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Auto Loading in PHP","og_description":"Read the blog to learn how we can use any instance of class without including the class file.","og_url":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/","og_site_name":"Knowband Blog | Ecommerce Modules","article_publisher":"https:\/\/www.facebook.com\/Knowband","article_published_time":"2019-07-04T11:12:17+00:00","article_modified_time":"2019-07-12T06:08:14+00:00","author":"Akash Roshan","twitter_card":"summary_large_image","twitter_creator":"@KnowbandPlugin","twitter_site":"@KnowbandPlugin","twitter_misc":{"Written by":"Akash Roshan","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/#article","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/"},"author":{"name":"Akash Roshan","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/b6c3192deed20653bb1cbe0ab61c95e8"},"headline":"Auto Loading in PHP","datePublished":"2019-07-04T11:12:17+00:00","dateModified":"2019-07-12T06:08:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/"},"wordCount":146,"commentCount":0,"publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"keywords":["auto loading","PHP","PHP auto loading"],"articleSection":["Ecommerce"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/","url":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/","name":"Auto Loading in PHP","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#website"},"datePublished":"2019-07-04T11:12:17+00:00","dateModified":"2019-07-12T06:08:14+00:00","description":"Read the blog to learn how we can use any instance of class without including the class file.","breadcrumb":{"@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.knowband.com\/blog\/ecommerce-blog\/auto-loading-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.knowband.com\/blog\/fr\/"},{"@type":"ListItem","position":2,"name":"Auto Loading in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.knowband.com\/blog\/fr\/#website","url":"https:\/\/www.knowband.com\/blog\/fr\/","name":"Knowband Blog | Ecommerce Modules","description":"Magento, Prestashop, Opencart, Shopify","publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.knowband.com\/blog\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization","name":"Knowband Blog | Ecommerce Modules","url":"https:\/\/www.knowband.com\/blog\/fr\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","width":153,"height":61,"caption":"Knowband Blog | Ecommerce Modules"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Knowband","https:\/\/x.com\/KnowbandPlugin"]},{"@type":"Person","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/b6c3192deed20653bb1cbe0ab61c95e8","name":"Akash Roshan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","url":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","caption":"Akash Roshan"},"description":"Akash is a PHP developer and wants to share his knowledge through Blogs. He is currently growing in the IT industry while fulfilling his own goals.","url":"https:\/\/www.knowband.com\/blog\/author\/aroshan\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/comments?post=35171"}],"version-history":[{"count":5,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35171\/revisions"}],"predecessor-version":[{"id":40121,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/35171\/revisions\/40121"}],"wp:attachment":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/media?parent=35171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/categories?post=35171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/tags?post=35171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}