Page 2 of 3

Posted: Wed Jul 26, 2006 4:02 pm
by jamiel
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.

Posted: Wed Jul 26, 2006 4:09 pm
by jamiel
And by this I mean PHP's implementation of type hinting in general, not your specific example ;)

Posted: Wed Jul 26, 2006 4:11 pm
by daedalus__
That's fine and good but what if you have a class that is critical in the execution of a script, then you don't want your script to keep trying to execute, it would just lead to more errors, right? So you use type hinting and it dies right there.

I also think that it is much prettier.

Posted: Wed Jul 26, 2006 4:17 pm
by jamiel
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.

Posted: Wed Jul 26, 2006 4:27 pm
by daedalus__
:)

Posted: Wed Jul 26, 2006 4:30 pm
by Jenk
I've just updated...

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..
and to my surprise, I found the following error when I fired up my app (that worked fine before on 5.0.8):

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 5
So a quick print_r(get_declared_classes()); and I find these:

Code: 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
)
(amongst others of course)

Posted: Wed Jul 26, 2006 4:37 pm
by feyd
yep, they've been adding more and more exceptions since back then. :)

Posted: Wed Jul 26, 2006 4:38 pm
by Luke
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 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.
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.

Posted: Wed Jul 26, 2006 4:43 pm
by Jenk
feyd wrote:yep, they've been adding more and more exceptions since back then. :)
I'm happy, yet peeved at the same time! Looks like I'll have some explaining to do when my clients hosts update :lol:



Oh, and btw - I've only just realised you can't typehint primitives in php yet :oops:

Posted: Wed Jul 26, 2006 4:46 pm
by jamiel
Haha Jenk, I was just fiddling and realized the same. Daedulus, your original example that started this whole discussion failed because it was looking for a class called 'string' . It wasn't checking the variable type of the variable you were passing.

Posted: Wed Jul 26, 2006 4:50 pm
by daedalus__
it worked fine after i passed that function a string

Posted: Wed Jul 26, 2006 4:50 pm
by jamiel
Code Demonstration:

Code: Select all

class ClassA
{
   public function __construct(int $int) { }
}

$foo = new ClassA(1);
= Fail

Code: Select all

class int
{
	public function __construct() { }
}
		
$bar = new ClassA(new int());
= Pass

Posted: Wed Jul 26, 2006 4:53 pm
by daedalus__
i swear that i tested it and it worked earlier....

Posted: Wed Jul 26, 2006 4:54 pm
by Jenk

Code: Select all

function PrintString(string $string)
{
    print($string);
}

PrintString('a');
output:

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
:)

Posted: Wed Jul 26, 2006 4:59 pm
by Luke
:lol: :lol: