I have this method :
public function getInstance()
{
$classname = $this->getFullyQualifiedClassName();
return new $classname();
}
Is there a way to write this without the $classname variable, which gets used only once?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use Reflection:
But as far as I know, that will be a lot slower than the solution you already have. In a perfect world you could just write something like this:
But unfortunately PHP’s syntax isn’t very flexible, and therefore you have to create that ugly variable thats troubling you.
I just realized that there is another (very ugly) way to do it: eval:
I do not suggest that you used this though, because of the security risk that comes with using eval.