Posted: Wed Jul 26, 2006 4:02 pm
Jenk, might be neater, but I think it takes all the control away from you. If its not a string, in most cases I would want my function to return false or null, not Fatal my entire script.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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.
I'm happy, yet peeved at the same time! Looks like I'll have some explaining to do when my clients hosts updatefeyd wrote:yep, they've been adding more and more exceptions since back then.
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());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