PHP,如何将类引用传递给方法?

时间:2014-10-18 17:55:41

标签: php

assertTriggerThisException ('Exception');
assertTriggerThisException ('AnotherExceptionClass');

function assertTriggerThisException ($exceptionClassname)
{
    try
    {
        something what triggers an exception
    }
    catch ($$exceptionClassname $e) // error
    {
    }
}

所以我想传递对类本身的引用,但这会导致语法错误。我想说“dinamically”期待一个例外,有办法吗?

1 个答案:

答案 0 :(得分:1)

我认为你可以尝试这样的事情

$someClass = 'SomeException';

try
{
    $some->thing();
}
catch (Exception $e)
{
    switch (get_class($e))
    {
        case $someClass:
            echo 'Dynamic exception.';
            break;
        default:
            echo 'Normal exception.';
    }
}