{"id":753,"date":"2021-07-30T16:31:07","date_gmt":"2021-07-30T20:31:07","guid":{"rendered":"https:\/\/crossan007.dev\/blog\/?p=753"},"modified":"2021-07-30T16:31:32","modified_gmt":"2021-07-30T20:31:32","slug":"expose-namespaced-php-classes-at-the-root","status":"publish","type":"post","link":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/","title":{"rendered":"Expose Namespaced PHP Classes at the Root"},"content":{"rendered":"\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=crossan007\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n\n<p>First off, I sincerely hope that nobody ever finds this blog post useful.  Alas, I&#8217;m sure there is some soul out there in a situation similar to my own: hang in there friend.<\/p>\n\n\n\n<p>If you&#8217;ve been writing PHP for any length of time, you know how easy it is to write <strong>bad<\/strong> PHP: don&#8217;t bother with classes, include all your functions in a single 6,000 line file, and certainly don&#8217;t set up a folder structure in your git repository.  <\/p>\n\n\n\n<p>So, what do you do when you want to start bringing order to the chaos?<\/p>\n\n\n\n<p>I found myself in a situation where I had a few hundred PHP files in the root of a repository; each file defining a class.  Unfortunately, none were in a namespace.  <\/p>\n\n\n\n<p>Additionally, these were accessible through a manually-defined <code>spl_autoload_register<\/code> function which was part of a <code>common.php<\/code> file that got <code>require_once<\/code>&#8216;d from tons of different places.  Some of the places requiring this file were even outside the repository and completely unknown to me.  <\/p>\n\n\n\n<p>I wanted to clean things up, but without breaking unknown code. <\/p>\n\n\n\n<p>The first thing I wanted to do was establish a namespace for these classes and to introduce a strategic folder hierarchy.  A few google searches quickly proved my suspicion: you can&#8217;t present classes belonging to a namespace as if they were available at the root namespace (well, you can with <code>use<\/code>, but that is scoped only to the current file; I don&#8217;t want to change hundreds of files).  See <a href=\"https:\/\/stackoverflow.com\/questions\/19890310\/php-global-namespace-aliases\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> and <a href=\"https:\/\/www.php.net\/manual\/en\/language.namespaces.importing.php#language.namespaces.importing.scope\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><\/p>\n\n\n\n<p>This would be a problem if I want to move in-use classes inside a namespace.<\/p>\n\n\n\n<p>But then I discovered <a href=\"https:\/\/stackoverflow.com\/questions\/19890310\/php-global-namespace-aliases\/19890311#19890311\" target=\"_blank\" rel=\"noreferrer noopener\">this comment<\/a> and started musing: &#8220;hmmm, I can create a class in the root namespace that extends a class in a child namespace&#8221; and &#8220;<code>spl_autoload_register<\/code> lets me try to find a class before PHP gives up&#8221; and &#8220;what if I could enumerate all classes in a defined namespace and dynamically instantiate a root-namespace proxy?&#8221;<\/p>\n\n\n\n<p>And that&#8217;s exactly what I did:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ since we tons of legacy files requiring common.php, \n\/\/ create an autoload proxy for root-&gt;ModelV1 until \n\/\/ we can add \"use\" statements to all necessary locations\nspl_autoload_register(function ($class) {\n\tif (strpos($class,\"\\\\\")!== false) {\n\t\t\/\/ only attempt to autoload root-namespace'd classes\n\t\treturn false;\n\t}\n\t$namespacesAvailableAtRoot = array(\"ModelV1\");\n\tforeach ($namespacesAvailableAtRoot as $namespace) {\n\t\tif (class_exists(\t$namespace.\"\\\\\" . $class)) {\n\t\t\t$str = sprintf('Class %s extends %s\\%s {}', $class, $namespace, $class);\n\t\t\t\/\/ this is uugly.  Yes; we're eval-creating new PHP classes at runtime where \n\t\t\t\/\/ the new class exists in the root namespace and extends the class implementation from \n\t\t\t\/\/ ModelV1.\n\t\t\t\/\/ Borrowed from: https:\/\/stackoverflow.com\/a\/13504972\n\t\t\t\/\/ and from: https:\/\/stackoverflow.com\/a\/19890311\n\t\t\teval($str);\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n});<\/code><\/pre>\n\n\n\n<p>Again, I hope absolutely nobody finds this useful.  In fact, I hope to thoroughly expunge this code In the Near Future \u2122 (but we all know how that goes)<\/p>\n\n\n\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First off, I sincerely hope that nobody ever finds this blog post useful. Alas, I&#8217;m sure there is some soul out there in a situation similar to my own: hang in there friend. If you&#8217;ve been writing PHP for any length of time, you know how easy it is to write bad PHP: don&#8217;t bother &hellip; <a href=\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Expose Namespaced PHP Classes at the Root<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[297],"tags":[295,296,294,39],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Expose Namespaced PHP Classes at the Root - Charles&#039; Blog\" \/>\n<meta property=\"og:description\" content=\"First off, I sincerely hope that nobody ever finds this blog post useful. Alas, I&#8217;m sure there is some soul out there in a situation similar to my own: hang in there friend. If you&#8217;ve been writing PHP for any length of time, you know how easy it is to write bad PHP: don&#8217;t bother &hellip; Continue reading Expose Namespaced PHP Classes at the Root &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/\" \/>\n<meta property=\"og:site_name\" content=\"Charles&#039; Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-30T20:31:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-30T20:31:32+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"crossan007\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#website\",\"url\":\"https:\/\/crossan007.dev\/blog\/\",\"name\":\"Charles&#039; Blog\",\"description\":\"SharePoint | PowerShell | Exchange | SCCM | Ubuntu | PHP | JavaScript | A\/V Live Production | More...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/crossan007.dev\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#webpage\",\"url\":\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/\",\"name\":\"Expose Namespaced PHP Classes at the Root - Charles&#039; Blog\",\"isPartOf\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/#website\"},\"datePublished\":\"2021-07-30T20:31:07+00:00\",\"dateModified\":\"2021-07-30T20:31:32+00:00\",\"author\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979\"},\"breadcrumb\":{\"@id\":\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/crossan007.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Expose Namespaced PHP Classes at the Root\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979\",\"name\":\"crossan007\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/crossan007.dev\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g\",\"caption\":\"crossan007\"},\"url\":\"https:\/\/crossan007.dev\/blog\/author\/crossan007\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"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:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/","og_locale":"en_US","og_type":"article","og_title":"Expose Namespaced PHP Classes at the Root - Charles&#039; Blog","og_description":"First off, I sincerely hope that nobody ever finds this blog post useful. Alas, I&#8217;m sure there is some soul out there in a situation similar to my own: hang in there friend. If you&#8217;ve been writing PHP for any length of time, you know how easy it is to write bad PHP: don&#8217;t bother &hellip; Continue reading Expose Namespaced PHP Classes at the Root &rarr;","og_url":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/","og_site_name":"Charles&#039; Blog","article_published_time":"2021-07-30T20:31:07+00:00","article_modified_time":"2021-07-30T20:31:32+00:00","twitter_misc":{"Written by":"crossan007","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/crossan007.dev\/blog\/#website","url":"https:\/\/crossan007.dev\/blog\/","name":"Charles&#039; Blog","description":"SharePoint | PowerShell | Exchange | SCCM | Ubuntu | PHP | JavaScript | A\/V Live Production | More...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/crossan007.dev\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#webpage","url":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/","name":"Expose Namespaced PHP Classes at the Root - Charles&#039; Blog","isPartOf":{"@id":"https:\/\/crossan007.dev\/blog\/#website"},"datePublished":"2021-07-30T20:31:07+00:00","dateModified":"2021-07-30T20:31:32+00:00","author":{"@id":"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979"},"breadcrumb":{"@id":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/crossan007.dev\/blog\/php\/expose-namespaced-php-classes-at-the-root\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/crossan007.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"Expose Namespaced PHP Classes at the Root"}]},{"@type":"Person","@id":"https:\/\/crossan007.dev\/blog\/#\/schema\/person\/bd99569cd81332c8fd866d023848b979","name":"crossan007","image":{"@type":"ImageObject","@id":"https:\/\/crossan007.dev\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fff72c74fb6a0da29accf0db83ad4b4b?s=96&d=mm&r=g","caption":"crossan007"},"url":"https:\/\/crossan007.dev\/blog\/author\/crossan007\/"}]}},"_links":{"self":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/753"}],"collection":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":3,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions\/756"}],"wp:attachment":[{"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crossan007.dev\/blog\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}