Did you know you can do this?
Moderator: General Moderators
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
I've just updated...
In my apps, I like to use specific exceptions, ala Java.
Thus I have an exceptions file, like so:
and to my surprise, I found the following error when I fired up my app (that worked fine before on 5.0.8):
So a quick print_r(get_declared_classes()); and I find these:
(amongst others of course)
In my apps, I like to use specific exceptions, ala Java.
Thus I have an exceptions file, like so:
Code: Select all
class FileNotFoundException extends Exception {}
class ArrayIndexOutOfBoundsException extends Exception {}
class InvalidArgumentException extends Exception {}
//etc..Code: Select all
Fatal error: Cannot redeclare class invalidargumentexception in E:\Program Files\Apache Group\Apache2\htdocs\stkmart\includes\config\exceptions.inc.php on line 5Code: Select all
Array
(
[1] => Exception
[2] => ErrorException
[4] => com_exception
[9] => ReflectionException
[23] => DOMException
[76] => BadFunctionCallException
[77] => BadMethodCallException
[78] => DomainException
[79] => InvalidArgumentException
[80] => LengthException
[81] => OutOfRangeException
[82] => RuntimeException
[83] => OutOfBoundsException
[84] => OverflowException
[85] => RangeException
[86] => UnderflowException
[87] => UnexpectedValueException
)I believe the point is more to prevent that object from ever getting anything other than what's expected. It's sort of like require() and include(). Require throws a fatal error if it can't find the file, but if include can't find it, but your code continues... there are likely to be more errors.jamiel wrote:If my Object cannot instantiate, I want to handle the error gracefully. At no point should a user see a blank screen or even worse the error. I will rather throw an exception in my constructor if the parameters are not what im expecting.
The point isn't to throw the fatal error, it's to make sure that the class gets the right kind of information... and if it doesn't... there is a bug you need to work out.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
-
jamiel
- Forum Contributor
- Posts: 276
- Joined: Wed Feb 22, 2006 5:17 am
- Location: London, United Kingdom
Code Demonstration:
= Fail
= Pass
Code: Select all
class ClassA
{
public function __construct(int $int) { }
}
$foo = new ClassA(1);Code: Select all
class int
{
public function __construct() { }
}
$bar = new ClassA(new int());
Last edited by jamiel on Wed Jul 26, 2006 4:53 pm, edited 1 time in total.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Code: Select all
function PrintString(string $string)
{
print($string);
}
PrintString('a');Code: Select all
Fatal error: Argument 1 passed to PrintString() must be an object of class string, called in E:\Program Files\Apache Group\Apache2\htdocs\blah\test.php on line 9 and defined in E:\Program Files\Apache Group\Apache2\htdocs\blah\test.php on line 4