tags:

views:

1186

answers:

8

Python's convention is that variables are created by first assignment, and trying to read their value before one has been assigned raises an exception. PHP by contrast implicitly creates a variable when it is read, with a null value. This means it is easy to do this in PHP:

function mymodule_important_calculation() {
    $result = /* ... long and complex calculation ... */;
    return $resukt;
}

This function always returns null, and if null is a valid value for the functuion then the bug might go undetected for some time. The Python equivalent would complain that the variable resukt is being used before it is assigned.

So... is there a way to configure PHP to be stricter with variable assignments?

+4  A: 

There is no way to make it fail as far as I know, but with E_NOTICE in error_reporting settings you can make it throw a warning (well, a notice :-) But still a string you can search for ).

Vinko Vrsalovic
+6  A: 

PHP doesn't do much forward checking of things at parse time.

The best you can do is crank up the warning level to report your mistakes, but by the time you get an E_NOTICE, its too late, and its not possible to force E_NOTICES to occur in advance yet.

A lot of people are toting the "error_reporting E_STRICT" flag, but its still retroactive warning, and won't protect you from bad code mistakes like you posted.

This gem turned up on the php-dev mailing-list this week and I think its just the tool you want. Its more a lint-checker, but it adds scope to the current lint checking PHP does.

PHP-Initialized Google Project

There's the hope that with a bit of attention we can get this behaviour implemented in PHP itself. So put your 2-cents on the PHP mailing list / bug system / feature requests and see if we can encourage its integration.

Kent Fredric
It's not necessarily too late, or you are throwing untested code to the world?I agree that it's better to have it fail at parse time, but saying it's too late suggests that you are not even testing it :-)
Vinko Vrsalovic
Well, depends how good you are are writing test cases. And depends if your test cases have variable miss-typing instead ( which goes unnoticed ). What next, test cases for test cases?
Kent Fredric
No, just saying it may be too late instead of it will be too late is enough. Not very relevant though, I admit.
Vinko Vrsalovic
A: 

I'm pretty sure that it generates an error if the variable wasn't previously declared. If your installation isn't showing such errors, check the error_reporting() level in your php.ini file.

nickf
A: 

You can try to play with the error reporting level as indicated here: http://us3.php.net/error_reporting but I'm not sure it mention the usage of non initiated variable, even with E_STRICT.

gizmo
+1  A: 

Check out error reporting, http://php.net/manual/en/function.error-reporting.php

What you want is probably E_STRICT. Just bare in mind that PHP has no namespaces, and error reporting becomes global. Kind of sucks to be you if you use a 3rd party library from developers that did not have error reporting switched on.

A: 

There is something similar : in PHP you can change the error reporting level. It's a best practice to set it to maximum in a dev environnement. To do so :

Add in your PHP.ini:

error_reporting = E_ALL

Or you can just add this at the top of the file your are working on :

error_reporting(E_ALL);

This won't prevent your code from running but the lack of variable assignments will display a very clear error message in your browser.

e-satis
Before PHP6, you need E_ALL | E_STRICT, otherwise you're not getting "ALL" of them. :)
Kent Fredric
A: 

I asked a similar question:

Php error_reporting, Best setting for development? E_STRICT ?

SeanDowney
A: 

If you use the "Analyze Code" on files, or your project in Zend Studio it will warn you about any uninitialized variables (this actually helped find a ton of misspelled variables lurking in seldom used portions of the code just waiting to cause very difficult to detect errors). Perhaps someone could at that functionality in the PHP lint function (php -l), which currently only checks for syntax errors.

Kris Erickson