summaryrefslogtreecommitdiff
path: root/src/PHP-5.3:Class_Exception_Not_Found.ascii
blob: 97128fe95625cdf1834f5592c7427559d0d49a54 (plain)
    1 PHP-5.3:Class Exception Not Found
    2 =================================
    3 :author: Aaron Ball
    4 :email: nullspoon@iohq.net
    5 
    6 == {doctitle}
    7 
    8 If in PHP 5.3 (likely just PHP 5) you define a namespace for your current class
    9 and try to throw a new Exception, it will fail with the following error
   10 
   11 ----
   12 Fatal error: Class 'your\current\class\namespace\Exception' not found in YourClass.php on line eleventy-billion.'
   13 ----
   14 
   15 The problem is that when PHP fails to find a class in the current namespace
   16 (other than root of course), it doesn't automagically search the root
   17 namespace. The Exception object exists in the root namespace (unless you
   18 created your own) so PHP won't find an it because it doesn't exist in your
   19 class's defined namespace.
   20 
   21 The solution is to define the root namespace before your new Exception
   22 object.
   23 
   24 ----
   25 throw new \Exception('This will do nicely');
   26 ----
   27 
   28 Category:PHP
   29 
   30 
   31 // vim: set syntax=asciidoc:

Generated by cgit