"strict_types" should be renamed "raise_type_error". WAS: About declare(strict_types = 1)
| From: | Yasuo Ohgaki | Date: | Mon, 16 Mar 2015 19:53:34 +0000 |
| Subject: | "strict_types" should be renamed "raise_type_error". WAS: About declare(strict_types = 1) | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-85083@lists.php.net to get a copy of this message | ||
Hi all,
I think this is important, but not many people realize the importance.
Therefore I created this as a new thread at the last minutes of vote.
On Mon, Mar 16, 2015 at 2:49 PM, Dennis Birkholz <dennis@birkholz.biz>
wrote:
> Am 16.03.2015 um 06:28 schrieb Xinchen Hui:
> > lib.php
> > <?php
> > declare(strict_types = 1);
> > function add(int $a, int $b) {
> > }
> >
> > <?php
> > add($_GET['a'], $_GET['b']);
> >
> > that means, I need to add a lots of (int) while I try to call a
> > function in a library which is not written by myself.
>
> that is not right and has been discussed a thousand times over.
> The declare changes the rules only for function calls in the file it is
> declared in.
>
> so:
> lib.php:
> <?php
> declare(strict_types = 1);
> function foo(int $a) {
> // no function call here
> }
> ?>
> The declare here does just nothing.
>
> <?php
> require "lib.php";
> foo("123"); // will work
> ?>
>
> <?php
> declare(strict_types = 1);
> require "lib.php";
> foo("123"); // will give an error
> ?>
>
>
If this kind of behavior is allowed, why "strict mode" is called strict?
It's not
strict at all if mode could be overridden.
"strict_mode" is just controlling errors, then it should be named as error
controlling directive and raise E_WARNING/E_TYPE or whatever.
Even if what its controlling is error that can be overridden by caller, yet
calling it "strict_types" is not correct. Proper name would be something
like "raise_type_error".
Let's see how it looks if "strict_types" is renamed to "raise_type_error"
<?php
declare(raise_type_error = 1);
function foo(int $a) {
// no function call here
}
?>
The declare here does just nothing.
<?php
require "lib.php";
foo("123"); // will work
?>
<?php
declare(raise_type_error = 1);
require "lib.php";
foo("123"); // will give an error
?>
Is everyone feel OK with this??
There are other things I would like to mention, but this would be enough
to change decision hopefully.
Regards,
P.S. If we are going to introduce strict types for basic types, what we
really need is type affinity for inputs. e.g. $_GET/$_POST/$_COOKIE.
https://www.sqlite.org/datatype3.html
--
Yasuo Ohgaki
yohgaki@ohgaki.net