Questions
- Is it possible to instantiate an class with the
get_class
method in PHP? - Is it possible to use
get_class
into ancatch()
block to catch an exception from that type of error?
My own try
Whenever using the get_class
, it returns the class name as a string. When I tried to instantiate it like this:
$classWhereExceptionObjectIsStored = new classWhereExceptionObjectIsStored();
try {
//Some code
} catch(get_class($classWhereExceptionObjectIsStored->getExceptionObject $e)) {
//Do stuff with the exception
}
It didnt work.
The class:
class classWhereExceptionObjectIsStored
{
public function getExceptionObject($message) {
return new LogicException($message); //For example
}
}
Second try
$class = get_class($classWhereExceptionObjectIsStored->getExceptionInstance('hi!'));
try {
//Some code
} catch($class $e)) {
//Do stuff with the exception
}