{"id":86061,"date":"2017-06-10T13:37:15","date_gmt":"2017-06-10T13:37:15","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=86061"},"modified":"2021-07-16T11:14:41","modified_gmt":"2021-07-16T11:14:41","slug":"autoloading-in-php","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/autoloading-in-php\/","title":{"rendered":"Autoloading in PHP"},"content":{"rendered":"\n<p>Today we are going to learn how autoloader works in PHP. Let\u2019s say we have a &nbsp;file student.php which have a class name student I want to use this class instance somewhere in other files. For example,&nbsp;I am using index.php if I want to&nbsp;create an instance of the student class then first I have to include student.php., These are the functions. We can use this functions to include code from other files.<\/p>\n\n\n\n<p>include<br>\ninclude_once<br>\nrequire<br>\nrequire_once<\/p>\n\n\n\n<p>If we want to use the class student in the index, we should do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted brush:php\">&lt;?php\n\/\/We are here in index.php\n\n\/\/this will include student.php in current file \ninclude \"student.php\";\n\n\/\/here i have created instance for student this class is created in student.php \n$student1 = new student();\n\n<\/pre>\n\n\n\n<p>So far seems easy to do this stuff. Now if we have to include more than one file then we will write include function to each and every file to include them inside index file. Now this is normal process without an autoloader<\/p>\n\n\n\n<pre class=\"wp-block-preformatted brush:php\">&lt;?php\n\ninclude \"student.php\";\ninclude \"marks.php\";\ninclude \"teacher.php\";\n..\n. \/\/there are more files to include we will write for each in same way \n\/\/this will take to much time and space if number of files are more\n.\n.\ninclude \"exam.php\";\n\n\/\/create instance for each class         \n$john = new student();<\/pre>\n\n\n\n<p>This is usual way for files to be loaded. Same task we can perform with autoloader in php but we will reduce some code which will save your time. Autoloader does &nbsp;same as the name says, it automatically loads a class whenever they are needed.<\/p>\n\n\n\n<p>Look at this example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted brush:php\">&lt;?php\n\/\/create function which will first store the path of that into a file variable \nfunction auto_loader($class)\n{\n$file = \"{$class}.php\";\n  \/\/this will check if file exist \n  if (is_file($file)) {\n   \/\/finally if file exist then it will include the file\n   include $file;\n  }\n}\n\nspl_autoload_register(\"auto_loader\");\n\n$john = new student(); \/\/ File will be autoloaded here<\/pre>\n\n\n\n<p>This is a very easy process. I have created a function name auto_loader() and it receives a class name to be the load. Inside this&nbsp;function, I have created a variable $file with the full path to the file to be included. Then I have&nbsp;checked if the file exists, if the file exists then it will include the file. Then I used the spl_autoload_register() function to let PHP know that it can also use auto_loader() to find a class. At last, I have created an instance of class student now all these above processes will take place. It will only load file student which have class name student, not all the files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we are going to learn how autoloader works in PHP. Let\u2019s say we have a &nbsp;file student.php which have a class name student I want to use this class instance somewhere in other files. For example,&nbsp;I am using index.php if I want to&nbsp;create an instance of the student class then first I have to <a href=\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":148,"featured_media":84482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[2071,2057],"class_list":["post-86061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-opencart","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Autoloading in PHP - Webkul Blog<\/title>\n<meta name=\"description\" content=\"PHP, autoloader, auto loder , loder\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Autoloading in PHP - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"PHP, autoloader, auto loder , loder\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-10T13:37:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-16T11:14:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mangesh Yadav\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mangesh Yadav\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\"},\"author\":{\"name\":\"Mangesh Yadav\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19\"},\"headline\":\"Autoloading in PHP\",\"datePublished\":\"2017-06-10T13:37:15+00:00\",\"dateModified\":\"2021-07-16T11:14:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\"},\"wordCount\":319,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"keywords\":[\"opencart\",\"PHP\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\",\"url\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\",\"name\":\"Autoloading in PHP - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"datePublished\":\"2017-06-10T13:37:15+00:00\",\"dateModified\":\"2021-07-16T11:14:41+00:00\",\"description\":\"PHP, autoloader, auto loder , loder\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/autoloading-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png\",\"width\":\"825\",\"height\":\"260\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/autoloading-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Autoloading in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19\",\"name\":\"Mangesh Yadav\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Mangesh Yadav\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/mangesh-yadav079\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Autoloading in PHP - Webkul Blog","description":"PHP, autoloader, auto loder , loder","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:\/\/webkul.com\/blog\/autoloading-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Autoloading in PHP - Webkul Blog","og_description":"PHP, autoloader, auto loder , loder","og_url":"https:\/\/webkul.com\/blog\/autoloading-in-php\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-06-10T13:37:15+00:00","article_modified_time":"2021-07-16T11:14:41+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","type":"image\/png"}],"author":"Mangesh Yadav","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Mangesh Yadav","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/"},"author":{"name":"Mangesh Yadav","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19"},"headline":"Autoloading in PHP","datePublished":"2017-06-10T13:37:15+00:00","dateModified":"2021-07-16T11:14:41+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/"},"wordCount":319,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","keywords":["opencart","PHP"],"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/autoloading-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/","url":"https:\/\/webkul.com\/blog\/autoloading-in-php\/","name":"Autoloading in PHP - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","datePublished":"2017-06-10T13:37:15+00:00","dateModified":"2021-07-16T11:14:41+00:00","description":"PHP, autoloader, auto loder , loder","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/autoloading-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/Code-Snippet.png","width":"825","height":"260"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/autoloading-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Autoloading in PHP"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ed1951d709bbfceb1fbf53ae003eac19","name":"Mangesh Yadav","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/de88cb7793914e715061efdd2f175586c8f16df48f6cf0cf9b12751abf72b1e2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Mangesh Yadav"},"url":"https:\/\/webkul.com\/blog\/author\/mangesh-yadav079\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/148"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=86061"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86061\/revisions"}],"predecessor-version":[{"id":296440,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/86061\/revisions\/296440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/84482"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=86061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=86061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=86061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}