This simple example won’t work, gives me:
Fatal error: spl_autoload() [function.spl-autoload]: Class GmailServer
could not be loaded.
define('USERNAME', 'username');
define('PASSWORD', 'password');
$SERVER = 'GmailServer';
spl_autoload_extensions(".php");
spl_autoload_register();
use Service\Mail\GmailServer;
$server = new $SERVER(USERNAME, PASSWORD);
While, of course, this is working:
$server = new GmailServer(USERNAME, PASSWORD);
Am i’m missing something?
EDIT: Working with reflection (but you HAVE to specify the full namespace):
$reflector = new \ReflectionClass("Service\\Mail\\$SERVER");
$server = $reflector->newInstance(USERNAME, PASSWORD);
Is it possible to run this?
If it does, it might be namespace related. If not, and also, I’d rather do that than using this quirk, use a factory pattern: