php - Using get_class in try - catch block -


questions

  • is possible instantiate class get_class method in php?
  • is possible use get_class catch() block catch exception type of error?

my own try

whenever using get_class, returns class name string. when tried instantiate this:

$classwhereexceptionobjectisstored = new classwhereexceptionobjectisstored();  try {    //some code  } catch(get_class($classwhereexceptionobjectisstored->getexceptionobject $e)) {    //do stuff 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 exception } 

get_class returns name of class, that's use of function. can instantiate class dynamically doing:

new $class(); 

which in case have result in like:

new get_class($classwhereexceptionobjectisstored->getexceptioninstance('hi!')); 

this doesn't work in catch block. that's because don't instantiate in catch block - thing specify there exception class you're expecting get.

an alternative catch exceptions:

catch(exception $e){} 

and within catch use php function is_a check if thrown exception of type you're expecting, so:

if(is_a($e, get_class($classwhereexceptionobjectisstored->getexceptioninstance('hi!'))){} 

however, question motives. can't think of use case add functionality, readability, scalability or usability.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -