namespace Exception

<?php
//example04.php
/*When catching an exception inside a namespace it is
important that you escape to 
the global space:
如何逃离出命名空间*/
namespace SomeNamespace;
class SomeClass
{
    function SomeFunction()
    {
        try
        {
            throw new \Exception('Some Error Message');
        }
        catch (\Exception $e)
        {
            var_dump($e->getMessage()); 
        }
    }
}
//报错://Fatal error: Class 'SomeNamespace/Exception' not found in ...
$o=new SomeClass;
$o->SomeFunction();

/*
string 'Some Error Message' (length=18)
*/

 

posted @ 2017-06-22 14:35  sky20080101  阅读(97)  评论(0)    收藏  举报