# Type hints revisited [IllegalArgumentException instead of E_ERROR]

- Thread: https://externals.io/message/368
- Original archive: https://news-web.php.net/php.internals/368
- Started by Timm Friebe on 2003-03-27T16:16:04+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #368 — Timm Friebe — 2003-03-27T16:16:04+00:00

URL: https://externals.io/message/368#368

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

---

## #371 — Andi Gutmans — 2003-03-27T16:58:03+00:00

URL: https://externals.io/message/368#371 · In reply to: #368

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(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

---

## #376 — Brian Moon — 2003-03-27T17:32:59+00:00

URL: https://externals.io/message/368#376 · In reply to: #368

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
|


----------------------------------------------------------------------------
----


| --
| 
|

---

## #377 — Timm Friebe — 2003-03-27T17:40:26+00:00

URL: https://externals.io/message/368#377 · In reply to: #376

> 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

---

## #379 — Brian Moon — 2003-03-27T17:45:21+00:00

URL: https://externals.io/message/368#379 · In reply to: #377

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

---

## #439 — Timm Friebe — 2003-03-28T17:15:27+00:00

URL: https://externals.io/message/368#439 · In reply to: #368

> 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

---

## #457 — Zeev Suraski — 2003-03-29T12:10:49+00:00

URL: https://externals.io/message/368#457 · In reply to: #368

At 07:15 29/03/2003, Timm Friebe wrote:
>
> > […]
>[...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.

?!

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

---

## #459 — Timm Friebe — 2003-03-29T13:23:29+00:00

URL: https://externals.io/message/368#459 · In reply to: #457

[...]
> > […]
> 
> ?!
> 
> 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
