Skip to content

Instantly share code, notes, and snippets.

@estabij
Forked from cmbuckley/Circle.php
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estabij/aa0a8b8e9deb53bc7ffc to your computer and use it in GitHub Desktop.
Save estabij/aa0a8b8e9deb53bc7ffc 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