Re: Is it a good idea to have a "strict" mode?
| From: | Alain Williams | Date: | Fri, 30 Jan 2015 10:27:03 +0000 |
| Subject: | Re: Is it a good idea to have a "strict" mode? | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81418@lists.php.net to get a copy of this message | ||
On Fri, Jan 30, 2015 at 11:16:41AM +0100, Martin Keckeis wrote:
> >
> > You might get more approval for: declare(strict=1);
> >
> > I would like to see this to help catch typeos in variable names. If a
> > variable
> > is assigned and it does not already exist some sort of error would be
> > raised.
> > The inspiration of this is from Perl where 'use strict' does this.
> >
> > How to declare a variable ? The keyword 'var' exists, it is only used in
> > classes
> > but could be used to declare variables in functions and at the top level.
> >
> > Thus:
> >
> > declare(strict=1);
> >
> > var $foo; // Declared but not given a value - so give it NULL
> > var $bar = 1;
> >
> > $foo = 234;
> >
> > $baz = 10; // This would error - $baz is a typeo for $bar
> >
> >
> You can achieve that already quiet simple
> <?php
> error_reporting(E_ALL);
>
> set_error_handler(function($errno, $errstr, $errfile, $errline){
> throw new \Exception($errstr);
> });
>
> $a = $b;
That catches the use of an undefined variable $b.
What I am talking about is assignment to an *undeclared* variable $baz (or $a).
This is not caught by your code - PHP does not have a concept of declaring
variables.
--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h>