class定义为另一个类的静态成员

I do this type of thing in Java all the time, and I'm trying to figure out if it's possible in PHP. This is what I would expect the syntax to look like if it was possible, but I'm wondering if there's some special way to do it (if it's possible, that is).

class Foo {
   public static class FooBarException extends Exception {
   }
   public static class BarBazException extends Exception {
   }

   public function DoSomething() {
      try {
          // Calls to other class methods that can
          // throw FooBarException and BarBazException
      } catch (self::FooBarException $e) {
          // Stuff..
      }
   }
}

$bang = new Foo();
try {
   $bang->DoSomething();
} catch (Foo::BarBazException $e) {
   // Stuff..
}

No, you can not. However, introduced in PHP 5.3 are namespaces. With namespaces you could similarly do:

<?php
namespace MyNamespace
{
    use Exception;

    class FooBarException
        extends Exception
    {
    }

    class FooBazException
        extends Exception
    {
    }

    class Foo
    {
        public function doSomething()
        {
            throw new FooBarException;
        }
    }
}

namespace AnotherNamespace
{
    $bang = new \MyNamespace\Foo;
    try {
        $bang->doSomething();
    } catch(\MyNamespace\FooBarException $up) {
        throw $up; // :)
    }
}

No, it is not possible.

You can use namespaces for a similar effect, however.

You cann't do it in PHP, but in php 5.3 you can use namespaces for similar functionality

http://php.net/namespace