I've implemented an additional feature for type hints that will throw an
exception instead of bailing out in case an incorrect type is passed.
Test script:
<?php
class Date { }
class Article {
public function setCreated_at(Date $date) {
echo CLASS, '::', FUNCTION, ' called with ';
var_export($date);
echo "\n";
}
public function setLastchange([Date] $date) {
echo __CLASS__, '::', __FUNCTION__, ' called with ';
var_export($date);
echo "\n";
}
}
$a= new Article();
$a->setLastchange(new Date());
$a->setLastchange(NULL); // Passes
$a->setCreated_at(new Date());
try {
$a->setCreated_at(NULL); // Fails
} catch (IllegalArgumentException $e) {
echo "Caught: "; var_dump($e);
}
$a->setCreated_at(1); // Fails
echo "Alive"; // Will not show up
?>
Output:
thekid@friebes:~/devel/php > ./php5/sapi/cli/php hints.php
article::setlastchange called with class date {
}
article::setlastchange called with NULL
article::setcreated_at called with class date {
}
Caught: object(illegalargumentexception)#2 (3) {
["message"]=>
string(38) "Argument 1 must be an instance of date"
["file"]=>
string(36) "/usr/home/thekid/devel/php/hints.php"
["line"]=>
int(4)
}
Fatal error: Uncaught exception! in Unknown on line 0
A unified diff is attached.
- Timm
At 05:16 PM 3/27/2003 +0100, Timm Friebe wrote:
I've implemented an additional feature for type hints that will throw an
exception instead of bailing out in case an incorrect type is passed.
I don't see any major advantage in doing this. I think we should keep PHP
error handling the same as in PHP 4 and leave exceptions in user-land.
Otherwise we'll end up having an unmanageable hybrid because there's no way
we're going to change the error-handling of the existing internal
functions. The majority of our user base is still functional, please don't
forget this. I feel that people here tend to forget that.
Andi
Test script:
<?php
class Date { }
class Article {
public function setCreated_at(Date $date) {
echo CLASS, '::', FUNCTION, ' called with ';
var_export($date);
echo "\n";
}public function setLastchange([Date] $date) { echo __CLASS__, '::', __FUNCTION__, ' called with '; var_export($date); echo "\n"; }}
$a= new Article();
$a->setLastchange(newDate());
$a->setLastchange(NULL); // Passes
$a->setCreated_at(newDate());try {
$a->setCreated_at(NULL); // Fails
} catch (IllegalArgumentException $e) {
echo "Caught: "; var_dump($e);
}$a->setCreated_at(1); // Fails
echo "Alive"; // Will not show up
?>Output:
thekid@friebes:~/devel/php > ./php5/sapi/cli/php hints.php
article::setlastchange called with class date {
}
article::setlastchange called withNULL
article::setcreated_at called with class date {
}
Caught: object(illegalargumentexception)#2 (3) {
["message"]=>
string(38) "Argument 1 must be an instance of date"
["file"]=>
string(36) "/usr/home/thekid/devel/php/hints.php"
["line"]=>
int(4)
}Fatal error: Uncaught exception! in Unknown on line 0
A unified diff is attached.
- Timm
Has type hinting for functions been implemented in ZE2?
Brian.
dealnews.com
----- Original Message -----
From: "Timm Friebe" thekid@thekid.de
To: engine2@lists.zend.com
Cc: internals@lists.php.net
Sent: Thursday, March 27, 2003 10:16 AM
Subject: [PHP-DEV] Type hints revisited [IllegalArgumentException instead of
E_ERROR]
| I've implemented an additional feature for type hints that will throw an
| exception instead of bailing out in case an incorrect type is passed.
|
| Test script:
|
| <?php
| class Date { }
| class Article {
| public function setCreated_at(Date $date) {
| echo CLASS, '::', FUNCTION, ' called with ';
| var_export($date);
| echo "\n";
| }
|
| public function setLastchange([Date] $date) {
| echo CLASS, '::', FUNCTION, ' called with ';
| var_export($date);
| echo "\n";
| }
| }
|
| $a= new Article();
| $a->setLastchange(new Date());
| $a->setLastchange(NULL); // Passes
| $a->setCreated_at(new Date());
|
| try {
| $a->setCreated_at(NULL); // Fails
| } catch (IllegalArgumentException $e) {
| echo "Caught: "; var_dump($e);
| }
|
| $a->setCreated_at(1); // Fails
| echo "Alive"; // Will not show up
| ?>
|
| Output:
| ---------------------------------------------------------------------
| thekid@friebes:~/devel/php > ./php5/sapi/cli/php hints.php
| article::setlastchange called with class date {
| }
| article::setlastchange called with NULL
| article::setcreated_at called with class date {
| }
| Caught: object(illegalargumentexception)#2 (3) {
| ["message"]=>
| string(38) "Argument 1 must be an instance of date"
| ["file"]=>
| string(36) "/usr/home/thekid/devel/php/hints.php"
| ["line"]=>
| int(4)
| }
|
| Fatal error: Uncaught exception! in Unknown on line 0
| ---------------------------------------------------------------------
|
| A unified diff is attached.
|
| - Timm
|
| --
|
|
Has type hinting for functions been implemented in ZE2?
Type hints apply to global functions just as they do for class methods.
<?php
class Date {
private
$utime = 0;
public function __construct($arg= -1) {
if (is_int($arg)) {
$this->utime= $arg < 0 ? `time()` : $arg;
} else {
$this->utime= strtotime($arg);
}
}
public function toString($fmt= 'r') {
return date($fmt, $this->utime);
}
}
function print_date(Date $date, $fmt) {
echo $date->toString($fmt);
}
print_date(new Date(), $argv[1]);
echo "\n";
?>
thekid@friebes:~/devel/php > ./php5/sapi/cli/php date.php "Y-m-d"
2003-03-27
- Timm
Ok, sorry, I have been off the ZE2 list for a while (just joined back).
Where is there more info on what the type hints do in ZE2. I am curious as
I wrote the RFC.
Brian.
dealnews
----- Original Message -----
From: "Timm Friebe" thekid@thekid.de
To: "Brian Moon" brianm@dealnews.com
Cc: engine2@lists.zend.com; internals@lists.php.net
Sent: Thursday, March 27, 2003 11:40 AM
Subject: Re: [PHP-DEV] Type hints revisited [IllegalArgumentException
instead of E_ERROR]
| On Thu, 2003-03-27 at 18:32, Brian Moon wrote:
| > Has type hinting for functions been implemented in ZE2?
|
| Type hints apply to global functions just as they do for class methods.
|
| <?php
| class Date {
| private
| $utime = 0;
|
| public function __construct($arg= -1) {
| if (is_int($arg)) {
| $this->utime= $arg < 0 ? time() : $arg;
| } else {
| $this->utime= strtotime($arg);
| }
| }
|
| public function toString($fmt= 'r') {
| return date($fmt, $this->utime);
| }
| }
|
| function print_date(Date $date, $fmt) {
| echo $date->toString($fmt);
| }
|
| print_date(new Date(), $argv[1]);
| echo "\n";
| ?>
| thekid@friebes:~/devel/php > ./php5/sapi/cli/php date.php "Y-m-d"
| 2003-03-27
|
| - Timm
|
|
| --
|
|
|
|
|
I've implemented an additional feature for type hints that will throw an
exception instead of bailing out in case an incorrect type is passed.
[...LONG disussion...]
After reading through a bunch of mails this generated, I get the idea
that most people here would be happier with an E_WARNING and the
function not being executed.
That gives:
- the OOP purist the possibility to throw an exception (in a userland
error handler) - the SOAP server a way to nicely recover
- the API designer a way to tell the user (s)he's messed up
- the "standard" programmer what (s)he's used to (PHP is quite
forgiving in the way it handles incorrect argument types and
usually warns about this)
Attached is the E_WARNING variant, an example script and its output.
- Timm
At 07:15 29/03/2003, Timm Friebe wrote:
I've implemented an additional feature for type hints that will throw an
exception instead of bailing out in case an incorrect type is passed.
[...LONG disussion...]After reading through a bunch of mails this generated, I get the idea
that most people here would be happier with anE_WARNINGand the
function not being executed.
?!
How the heck can we even think about such a thing? When you call a
function, you expect it to run. The code that follows it may rely on stuff
that it has done. Not running it is simply not an option, I can't even
begin to imagine the possible consequences of such an approach!
Type hints are shortcuts for instanceof. If you want to handle a situation
where the function is passed the wrong arguments, don't use type hints, use
instanceof. Or use the errors-for-exceptions mode that we may have.
Zeev
[...]
After reading through a bunch of mails this generated, I get the idea
that most people here would be happier with anE_WARNINGand the
function not being executed.?!
How the heck can we even think about such a thing? When you call a
function, you expect it to run. The code that follows it may rely on stuff
that it has done. Not running it is simply not an option, I can't even
begin to imagine the possible consequences of such an approach!
Well, at the moment, the function is not run either, isn't it? The
program dies.
To clarify:
function foo(Bar $bar) {
// [...]
}
is - with my patch - equivalent to:
function foo($bar) {
if (!($bar instanceof Bar)) {
trigger_error('Argument 1 must be an instance of Bar', E_WARNING);
return;
}
// [...]
}
and basically much nicer as a function containing - say - five or six
arguments all needing to be checked in the "not-instance-of" manner.
At the moment, it is:
function foo($bar) {
if (!($bar instanceof Bar)) {
die('Argument 1 must be an instance of Bar');
}
// [...]
}
That's all I changed.
Type hints are shortcuts for instanceof. If you want to handle a situation
where the function is passed the wrong arguments, don't use type hints, use
instanceof. Or use the errors-for-exceptions mode that we may have.
I guess, as we can't find a consensus here, that is what has to be done.
- Timm