{"id":49,"date":"2010-01-25T00:31:02","date_gmt":"2010-01-25T08:31:02","guid":{"rendered":"http:\/\/patternweaver.com\/blog\/?p=49"},"modified":"2010-01-25T00:31:02","modified_gmt":"2010-01-25T08:31:02","slug":"php-autoloader","status":"publish","type":"post","link":"http:\/\/patternweaver.com\/blog\/2010\/01\/php-autoloader\/","title":{"rendered":"PHP Autoloader"},"content":{"rendered":"<p>When working on a large project with a number of backend tasks, intranet and public websites, I found that a couple of third party libraries had conflicting names, so when the autoloader ran, it always found the one it hit first. I found myself wondering if it would be possible to Autoload by scope, by registering each registry to the file that had the declaration. That way if a directory was declared in a given file, it will always prefer that, and thus load the <b>right<\/b> instance. It, unfortunately, does *not* solve your problem if you  need to use both instances inside of a single execution.<\/p>\n<p>The second feature is due to the fact the autoloader executes outside the normal program scope, you wind up not being able to catch an error classloading&#8230; it just goes fatal. So to work around that, I build a dummy object that throws an exception on construction, so that it happens inside of a scope it can be caught in.<\/p>\n<pre><code>_import('path.to.package.*');<\/code><\/pre>\n<p>or<\/p>\n<pre><code>Autoloader::register_directory('.\/path\/to\/package\/');<\/code><\/pre>\n<p>Download the <a href=\"http:\/\/patternweaver.com\/Downloads\/Autoloader.zip\">autoloader<\/a>.<\/p>\n<p>I tried to make it as simple and versatile as possible, and I&#8217;m looking into automatic namespaces. Ask if you have any questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working on a large project with a number of backend tasks, intranet and public websites, I found that a couple of third party libraries had conflicting names, so when the autoloader ran, it always found the one it hit first. I found myself wondering if it would be possible to Autoload by scope, by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":0,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}