Did you know you can do this?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

And by this I mean PHP's implementation of type hinting in general, not your specific example ;)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

:)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yep, they've been adding more and more exceptions since back then. :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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:
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

it worked fine after i passed that function a string
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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
Last edited by jamiel on Wed Jul 26, 2006 4:53 pm, edited 1 time in total.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

i swear that i tested it and it worked earlier....
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
:)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:lol: :lol:
Post Reply