Hei,
currently if you pass a wrong object's type to a typehinted parameter:
derick@kossu:~$ cat /tmp/foo.php
<?php
class foo {
function bar(foo $a)
{
}
}
$a = new foo;
$a->bar(new stdClass);
?>
derick@kossu:~$ php /tmp/foo.php
Fatal error: Argument 1 must be an instance of foo in /tmp/foo.php on
line 3
As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that case
having this fatal error to stop the whole application can be annoying.
Opinions?
Derick
Derick Rethans schrieb:
Opinions?
+1
--
Sebastian Bergmann http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
Derick Rethans schrieb:
Opinions?
+1
Does that mean you want more opinions? :)
regards,
Derick
Derick Rethans wrote:
Does that mean you want more opinions? :)
Throw an InvalidArgumentException from SPL...
--
Ondrej Ivanic
(ondrej@kmit.sk)
Derick Rethans wrote:
Does that mean you want more opinions? :)
Throw an InvalidArgumentException from SPL...
Won't work, SPL can be disabled.
Derick
Derick Rethans schrieb:
Won't work, SPL can be disabled.
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.
--
Sebastian Bergmann http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
Hi Sebastian Bergmann!
On 08/08/05 13:32 you wrote:
Won't work, SPL can be disabled.
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.
That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.
Regards,
Tobias Schlitt - Zend Certified Engineer GPG Key: 0xA6529579
a passion for php http://www.schlitt.info
Like to say "thank you"? - http://pear.php.net/wishlist.php/toby
Hi Toby,
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.
No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class. But by using nested Exceptions you can catch them
independently. Else you would have to catch every Exception, parse the error
message or trace to see wether it was a problem while calling the
function/method or some code inside the function/method went wrong.
<?php
function foo() {
$bar = ....;
try {
call_with_wrong_parameter($bar);
} catch (InvalidArgumentException $e) {
// bad function call...
}
}
try {
foo();
} catch (Exception $e) {
// any other exception
}
?>
I'd like to see something like what Sebastian suggested. And imho it wouldn't
really be a bc-break since an uncaught exception is fatal, too.
johannes
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class.
Throwing two different kinds of Exceptions is evil. It should be
consistent, not relying on an external component for it's exceptions. So
it will be a normal Exception. This is a useles branch of this tread -
and it should end now.
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.
--
Sebastian Bergmann http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.
Not really. SPL is only for people that actually want to use Java.
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
On Mon, 8 Aug 2005 15:09:06 +0200 (CEST)
derick@php.net (Derick Rethans) wrote:
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.
Not really. SPL is only for people that actually want to use Java.
Wrong, and this is a stupid argument, sorry.
But this is another topic.
--Pierre
Derick Rethans wrote:
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.Not really. SPL is only for people that actually want to use Java.
I thought that's what exceptions are for (-:C
Sorry, couldn't resist and I promise to shut up again,
- Chris "exceptions are only simple if you don't use them" Schneider
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.
Not really. SPL is only for people that actually want to use Java.
Enabling it by default is same as enabling unnecessary bloat by default.
So no thanks for making it not possible to disable..
--Jani
Hello Derick,
Monday, August 8, 2005, 3:09:06 PM, you wrote:
Derick Rethans schrieb:
Throwing two different kinds of Exceptions is evil.
Just as evil as allowing SPL to be disabled.
Not really. SPL is only for people that actually want to use Java.
The stupiest thing you could have come up with.
If it were about Java it was unusable and slow.
You see t hat's the same bullshit and doesn't belong here - very contra
productive.
Best regards,
Marcus
Hello Derick,
Monday, August 8, 2005, 3:00:50 PM, you wrote:
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class.
Throwing two different kinds of Exceptions is evil. It should be
consistent, not relying on an external component for it's exceptions. So
it will be a normal Exception. This is a useles branch of this tread -
and it should end now.
Throwing a pure Exception is useless. Since it is the nature of Exception
handling to code the reson into the Exception hierarchy. See my other reply
and the comment on moving the exceptions to the engine.
Best regards,
Marcus
Hi Johannes Schlueter!
On 08/08/05 14:50 you wrote:
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.
That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.
No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class. But by using nested Exceptions you can catch them
independently. Else you would have to catch every Exception, parse the error
message or trace to see wether it was a problem while calling the
function/method or some code inside the function/method went wrong.
The point is, that it's senseless to have it throw any exception that
can be disabled, when you want to write portable applications. In that
case you still have to stick to catch Exception and have no benefit of
it throwing anything else, when SPL is enabled.
I would pretty much appreciate it having thrown an
InvalidArgumentException, but then this should work everywhere.
Regards,
Tobias Schlitt - Zend Certified Engineer GPG Key: 0xA6529579
a passion for php http://www.schlitt.info
Like to say "thank you"? - http://pear.php.net/wishlist.php/toby
Tobias Schlitt wrote:
Hi Johannes Schlueter!
On 08/08/05 14:50 you wrote:The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class. But by using nested Exceptions you can catch them
independently. Else you would have to catch every Exception, parse the error
message or trace to see wether it was a problem while calling the
function/method or some code inside the function/method went wrong.The point is, that it's senseless to have it throw any exception that
can be disabled, when you want to write portable applications. In that
case you still have to stick to catch Exception and have no benefit of
it throwing anything else, when SPL is enabled.I would pretty much appreciate it having thrown an
InvalidArgumentException, but then this should work everywhere.
I guess creating a new special exception (e.g. TypeHintException) that
extends Exception as part of the php core (as opposed to living in SPL)
is a stupid idea?
IMHO btw, semantically, calling it S(tandard)PL and then making it so that
it's not standard (i.e. it's an extension) seems odd.
Regards,
On Mon, 08 Aug 2005 15:56:55 +0200
jochem@iamjochem.com (Jochem Maas) wrote:
IMHO btw, semantically, calling it S(tandard)PL and then making
it so that it's not standard (i.e. it's an extension) seems odd.
I was pretty sure until today that it was not possible to disable
it. But it's not possible to build dynamicly.
This is odd, to do not say stupid. What's about making really
standard?
Regards,
--Pierre
Hello Tobias,
Monday, August 8, 2005, 3:35:28 PM, you wrote:
Hi Johannes Schlueter!
On 08/08/05 14:50 you wrote:
The throw an Exception when SPL disabled and an InvalidArgumentException
when it is enabled.
That's senseless when writing applications that shall be version
independant. Just sticking to Exception should be fine.
No it is not, in your application you can still simply catch Exception to be
independent of SPL since the InvalidArgumentException class extends the
Exception class. But by using nested Exceptions you can catch them
independently. Else you would have to catch every Exception, parse the error
message or trace to see wether it was a problem while calling the
function/method or some code inside the function/method went wrong.
The point is, that it's senseless to have it throw any exception that
can be disabled, when you want to write portable applications. In that
case you still have to stick to catch Exception and have no benefit of
it throwing anything else, when SPL is enabled.
I would pretty much appreciate it having thrown an
InvalidArgumentException, but then this should work everywhere.
If you plan to support PHP builds without SPL then that most likley means
that you have dropped other built-in default extensions too. Probably
because of their memory space to reduce loading time. In that case you are
not up for portability or have made a major mistake already in the
beginning.
Anyway there is no argument here. If you go for portable apps in the sense
that any extension can be diabled then catching plain Exceptions should be
more than good enough. You cannot expect to have full blown oo support when
you disable the one oo extension.
As a side note we could probably also move all exception declarations from
SPL to the engine but then we'd loose the possibility to support builds
without SPL. At the end of the day i'd even like to drop reflection support
from the engine and move it to a specialized extensions - again - for
speed/memory reasons.
best regards
marcus
without SPL. At the end of the day i'd even like to drop reflection support
from the engine and move it to a specialized extensions - again - for
speed/memory reasons.
YES PLEASE! I've absolutely no use for it, and I really really need
every bit of memory/speed/whatever I can get out of PHP..
--Jani
Jani Taskinen wrote:
without SPL. At the end of the day i'd even like to drop reflection
support
from the engine and move it to a specialized extensions - again - for
speed/memory reasons.YES PLEASE! I've absolutely no use for it, and I really really need every bit of memory/speed/whatever I can get out of PHP..
Wow!
Is PHP the right tool for you?
If you don't use 00 things you can stay on PHP 4.x with ZE1. What you
need/don't from PHP?
It will be useful to move some parts of Exception hierarchy from SPL to
ZE2, but from PHP 5.1 SPL it will be use full to move everything :)
--
Ondrej Ivanic
(ondrej@kmit.sk)
Hi Marcus Boerger!
On 08/08/05 19:58 you wrote:
The point is, that it's senseless to have it throw any exception that
can be disabled, when you want to write portable applications. In that
case you still have to stick to catch Exception and have no benefit of
it throwing anything else, when SPL is enabled.
I would pretty much appreciate it having thrown an
InvalidArgumentException, but then this should work everywhere.
If you plan to support PHP builds without SPL then that most likley means
that you have dropped other built-in default extensions too. Probably
because of their memory space to reduce loading time. In that case you are
not up for portability or have made a major mistake already in the
beginning.
Anyway there is no argument here. If you go for portable apps in the sense
that any extension can be diabled then catching plain Exceptions should be
more than good enough. You cannot expect to have full blown oo support when
you disable the one oo extension.
That's the point: OO extension. I agree that things like iterators and
stuff belong into SPL, since they're not standard OO features. But
things like predefined exceptions belong into the engine. Especially
those that get thrown by a PHP builtin feauture.
Not to get me wrong, I'm strongly against having extensions itself or
functions throw exceptions, but in the case we currently discuss, we are
dealing with an OO feature of PHP, that should definitly throw it's own
exception. Why else were exceptions invented when not to be used with OO
features?
As a side note we could probably also move all exception declarations from
SPL to the engine but then we'd loose the possibility to support builds
without SPL. At the end of the day i'd even like to drop reflection support
from the engine and move it to a specialized extensions - again - for
speed/memory reasons.
Having all exceptions in the engine sounds much overhead. But some well
defined ones that get utilized by PHP OO features would definitly make
sense.
In the end, I have not a real problem with any solution, as long as OO
features start throwing exceptions instead of raising errors. :)
Regards,
Toby
Tobias Schlitt - Zend Certified Engineer GPG Key: 0xA6529579
a passion for php http://www.schlitt.info
Like to say "thank you"? - http://pear.php.net/wishlist.php/toby
Derick Rethans schrieb:
Does that mean you want more opinions? :)
No, I want a (InvalidArgument?)Exception to be thrown when I type-hint
is not met.
--
Sebastian Bergmann http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
+1
-----Original Message-----
From: Derick Rethans [mailto:derick@php.net]
Sent: Monday, August 08, 2005 11:50 AM
To: PHP Developers Mailing List
Subject: [PHP-DEV] type hinting throwing a fatal errorHei,
currently if you pass a wrong object's type to a typehinted parameter:
derick@kossu:~$ cat /tmp/foo.php
<?php
class foo {
function bar(foo $a)
{
}
}$a = new foo;
$a->bar(new stdClass);
?>
derick@kossu:~$ php /tmp/foo.phpFatal error: Argument 1 must be an instance of foo in /tmp/foo.php
on
line 3As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that case
having this fatal error to stop the whole application can be annoying.
Opinions?Derick
Derick Rethans wrote:
Hei,
currently if you pass a wrong object's type to a typehinted parameter:
[...]
Fatal error: Argument 1 must be an instance of foo in /tmp/foo.php on
line 3As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that
case having this fatal error to stop the whole application can be
annoying. Opinions?
As a PHP user, I have to say I wholeheartedly agree, and I'm glad someone is
raising this issue again. The last time it was seriously discussed:
http://marc.theaimsgroup.com/?l=php-dev&m=104878782529499&w=2
was less than encouraging.
As a user who is trying to write robust code, my biggest issue is not
whether or not a type hint violation throws an exception, but whether or not
it results in an error than I can trap for. An exception would be great,
obviously, but I'd be just as happy with an E_WARNING, so at the very least
my custom error handler can catch this. As you have pointed out, it's
currently a fatal error so a user-defined error handler is not called.
I don't typically comb through my server's php error logs, since I mainly
depend on my custom error handler to let me know when one of my applications
is having problems. Because of this I have been forced to avoid using type
hints and I've actually implemented by own function to simulate type hints,
like so:
/* @param SomeObject $foo */
public function someMethod($foo) {
checkArgType($foo, 'SomeObject');
}
checkArgType() throws an exception if $foo isn't instanceof 'SomeObject'.
I would MUCH rather use type hints here. They're cleaner from a
documentation standpoint and the wtf factor is much lower. Unfortunately I
can't as long as type hint violations are fatal.
So, I would be very happy if type hint violations either threw and exception
OR triggered an E_WARNING. As Wez pointed out in
http://marc.theaimsgroup.com/?l=php-dev&m=104911187824684&w=2, currently if
you pass the wrong number of arguments to a function/method you get an
E_WARNING
(not a fatal error), so I don't see why type hint violations
shouldn't be treated similarly. It would increase their value and utility
immensely...
Derick Rethans wrote:
As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that case
having this fatal error to stop the whole application can be annoying.
Opinions?
+1 on throwing an exception.
- Markus
Hmm,
I was complaining about inusaability ot type hinting because of this fatal
errors but nobody seems heard me :(. One better do a check in the code than
risking a fatal error which is unstoppable...
I call this showstopper if we say that we are proud with the type-hinting.
Andrey
Quoting Derick Rethans derick@php.net:
Hei,
currently if you pass a wrong object's type to a typehinted parameter:
derick@kossu:~$ cat /tmp/foo.php
<?php
class foo {
function bar(foo $a)
{
}
}$a = new foo;
$a->bar(new stdClass);
?>
derick@kossu:~$ php /tmp/foo.phpFatal error: Argument 1 must be an instance of foo in /tmp/foo.php on
line 3As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that case
having this fatal error to stop the whole application can be annoying.
Opinions?Derick
Hmm,
I was complaining about inusaability ot type hinting because of this fatal
errors but nobody seems heard me :(. One better do a check in the code than
risking a fatal error which is unstoppable...
I call this showstopper if we say that we are proud with the type-hinting.
Proof-of-concept patch is here:
http://files.derickrethans.nl/patches/typehint-reference-2005-08-08.diff.txt
(I need to clean up the message allocation stuff)
Derick
--
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
I don't think that's a good idea, regardless of implementation issues.
Calling to a function with the wrong arguments is something that should be
dealt with when developing the application, not at runtime. I think that
throwing exceptions in all sorts of places encourages people to write
'exception-oriented' apps, which is very messy. Type hinting is also not
exactly an OO thing, it's an object thing, and there's a difference. PHP
is filling up with a lot of builtin classes, as well as infrastructure
classes, that actually simplify the lives of users, without them having to
have a clue about object orientation. Some examples that come to mind are
SimpleXML, the SOAP classes and PDO. On a long enough timescale -
everybody using PHP will be using objects, and many (if not most) of them
will be using them in procedural apps. I see a big negative point in
forcibly introducing these people to the concept of exceptions.
I believe we mentioned once the possibility of adding another error level,
which is fatal - but still catchable by set_error_handler()
. That is a
good idea (which we should be doing either way).
Zeev
At 12:50 08/08/2005, Derick Rethans wrote:
Hei,
currently if you pass a wrong object's type to a typehinted parameter:
derick@kossu:~$ cat /tmp/foo.php <?php class foo { function bar(foo $a) { } } $a = new foo; $a->bar(new stdClass); ?> derick@kossu:~$ php /tmp/foo.php Fatal error: Argument 1 must be an instance of foo in
/tmp/foo.php on
line 3As type hinting is a new OO thing, it might perhaps make some sense to
make this an exception instead - as this error might also happen for
dynamic things by people who use the classes you designed. In that case
having this fatal error to stop the whole application can be annoying.
Opinions?Derick