Skip to content

Instantly share code, notes, and snippets.

@cmbuckley
Created May 11, 2012 08:18
Show Gist options
  • Save cmbuckley/2658317 to your computer and use it in GitHub Desktop.
Save cmbuckley/2658317 to your computer and use it in GitHub Desktop.
Example structure for http://stackoverflow.com/q/10542012
<?php
// file: ./bootstrap.php
spl_autoload_register(function($className) {
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
var_dump($fileName); // to see which file is loaded
require $fileName;
});
new Shape\Circle();
<?php
// file: ./Shape/Circle.php
namespace Shape;
class Circle extends Shape implements ShapeInterface {
public function __construct() {
var_dump($this);
}
}
<?php
// file: ./Shape/Shape.php
namespace Shape;
abstract class Shape {}
<?php
// file: ./Shape/ShapeInterface.php
namespace Shape;
interface ShapeInterface {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment