Skip to content

Instantly share code, notes, and snippets.

@edorian
Last active December 11, 2015 22:09
Show Gist options
  • Save edorian/4667851 to your computer and use it in GitHub Desktop.
Save edorian/4667851 to your computer and use it in GitHub Desktop.
edo@localhost ~/auto-all-the-loads $ tree && find . -type f -print -exec cat {} \;
.
|-- autoload.php
|-- index.php
`-- src
|-- A.php
`-- bar
|-- baz
| `-- B.php
`-- B.php
3 directories, 5 files
./src/A.php
<?php
namespace edo\foo;
class A {}
./src/bar/baz/B.php
<?php
namespace edo\foo\bar\baz;
class B {}
./src/bar/B.php
<?php
namespace edo\foo\bar;
class B {}
./index.php
<?php
namespace edo\foo;
require_once __DIR__ . '/autoload.php';
$x = new A();
$y = new \edo\foo\bar\B();
$z = new \edo\foo\bar\baz\B();
echo "Jojo";
./autoload.php
<?php
spl_autoload_register(function($className) {
$prefix = 'edo\\foo\\';
if (strncmp($prefix, $className, strlen($prefix)) === 0) {
require __DIR__ . '/src/' . str_replace('\\', '/', substr($className, strlen($prefix))) . '.php';
}
});
@edorian
Copy link
Author

edorian commented Jan 29, 2013

php index.php
string(38) "/home/edo/auto-all-the-loads/src/A.php"
string(42) "/home/edo/auto-all-the-loads/src/bar/B.php"
string(46) "/home/edo/auto-all-the-loads/src/bar/baz/B.php"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment