Re: Variable name declarations?

From: Date: Fri, 13 Jul 2001 20:38:41 +0000
Subject: Re: Variable name declarations?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-57801@lists.php.net to get a copy of this message
Hi Matthew, Set your error_reporting level to E_ALL. The parser will then report the use of undeclared variables. --zak ----- Original Message ----- From: "Matthew Aznoe" <maznoe@fuzztech.com> To: "php" <php-general@lists.php.net> Sent: Friday, July 13, 2001 2:22 PM Subject: [PHP] Variable name declarations? > > > I have to agree that strong typing is not a good fit with PHP. It really > limits the flexibility of the language that is, at least to me, one it its > strongest appeals. Often, strong typing can be overly restrictive, and free > typing, when combined with good comments and documentation which should go > alongside any piece of code, allows much easier development in my > experience. > > However, I do have one complaint about PHP that seems to have a tendency to > bite me far too often, and that is the lack of variable declaration. While > I do not think the type of the variable should be required, requiring the > user to specify which variable names are going to be used I think would be > useful. The reason why I believe this is important is in debugging. I have > been caught many times misspelling a variable name slightly (ie -> ei), and > since PHP does no variable name checking, sometimes it takes a while to > discover the error. If each variable name had to be declared prior to use, > it would eliminate this problem by warning the user each time an undeclared > name was used. > > The syntax could be very simple using constructs that already exist in the > langauge. The name would be defined by using the var command somewhere in > code prior to the variable being used (as in classes... but here again, the > class elements are not limited to those defined). The declaration would > reserve only the name of the variable, so the actual type would continue to > be free form. Arrays could continue to be created as they are now since > only the name of the variable would be checked (you could still have > potential problems in associative arrays). Global PHP variables (ie > $GLOBALS) would not need to be declared (they would be declared > automatically). > > example: > > function sum_array( $input_array ) > { > var $index; > var $sum = 0; > > for( $index = 0; $index < count( $input_array ); $index++ ) > $sum += $input_array[ $index ]; > > return $sum; > } > > Potentially, the $index variable could also be declared within the for loop > by adding a var before the first index as well. To keep things simple and > in line with the current variable scope rules, the scope of the name would > be global for the function in which it was defined. > > I do not know all of the logistics behind the parsing and compilation > processes in the PHP engine, but I believe this could be done without too > much overhead. It could be an optional feature (controlled from the php.ini > file) that could be turned off when released to production to save on > performance. If this value was defaulted to "off" in the release, it would > also allow people to upgrade existing code to the new version without > having to worry about instantly changing their code to the new paradigm. > > I do not believe that this would sacrifice very much freedom in the > language, and it would certainly make debugging and maintainence easier on > the developer. Also, by having all of the variables declared at the top of > the functions, it could help commenting by describing the variable name as > it is being declared, and it would encourage a better coding style. > > Does anyone else have any thoughts on this? > > > Matthew Aznoe > maznoe@fuzztech.com > > ---------------------------------------------------------------------------- ---- > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#57801) next »