Skip to content

Instantly share code, notes, and snippets.

@jayminpanchal
Created June 24, 2017 18:03
Show Gist options
  • Save jayminpanchal/cc3d782cba9880623f0a5701c359ec1a to your computer and use it in GitHub Desktop.
Save jayminpanchal/cc3d782cba9880623f0a5701c359ec1a to your computer and use it in GitHub Desktop.
Create Class object from class name
class ClassFactory
{
function __construct($namespace = '')
{
$this->namespace = $namespace;
}
public function make($source)
{
$name = $this->namespace . '\\' . $source;
if (class_exists($name)) {
return new $name();
}
}
}
$classFactory = new ClassFactory("App\\Model");
$user = $classFactory->make("User");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment