【问题标题】:Php, how to pass a class reference to a method? [duplicate]Php,如何将类引用传递给方法? [复制]
【发布时间】:2014-10-18 17:55:41
【问题描述】:
assertTriggerThisException ('Exception');
assertTriggerThisException ('AnotherExceptionClass');

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

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

【问题讨论】:

    标签: php


    【解决方案1】:

    我觉得你可以试试这样的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-02
      • 2013-07-15
      • 2011-09-26
      • 2014-05-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多