Reputation: 9323
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..
}
Upvotes: 2
Views: 92
Reputation: 1526
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; // :)
}
}
Upvotes: 3
Reputation: 1122
You cann't do it in PHP, but in php 5.3 you can use namespaces for similar functionality
Upvotes: 1
Reputation: 9335
No, it is not possible.
You can use namespaces for a similar effect, however.
Upvotes: 1