Re: [PHP] Variable name declarations?

2001-07-15 Thread teo

Hi Kent!
On Sat, 14 Jul 2001, Kent Sandvik wrote:

 
  function sum_array( $input_array )
  {
  var $index;
  var $sum = 0;
 
  for( $index = 0; $index  count( $input_array ); $index++ )
  $sum += $input_array[ $index ];
 
  return $sum;
  }
 
 The array variable issue has indeed bitten me a couple of times, so now all
 my arrays are called $something_arr. It would nice to introduce more
 runtime checking of array types and complaints in case non-array values are
 used in functions. But otherwise the strong type checking will just cause
 more problems and more overhead with calls. I don't know if the example
 above will help that much, but you could already write similar code in
 functions to make sure that the values are declared on top, to make things
 clearer.
 
you can add:
!is_array($input_array)  ($input_array = array($input_array);
before your for() loop, if the client of sum_array() doesn't guarentee to
provide only arrays.

-- teodor

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] Variable name declarations?

2001-07-14 Thread Kent Sandvik


 function sum_array( $input_array )
 {
 var $index;
 var $sum = 0;

 for( $index = 0; $index  count( $input_array ); $index++ )
 $sum += $input_array[ $index ];

 return $sum;
 }

The array variable issue has indeed bitten me a couple of times, so now all
my arrays are called $something_arr. It would nice to introduce more
runtime checking of array types and complaints in case non-array values are
used in functions. But otherwise the strong type checking will just cause
more problems and more overhead with calls. I don't know if the example
above will help that much, but you could already write similar code in
functions to make sure that the values are declared on top, to make things
clearer.

--Kent



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Variable name declarations?

2001-07-13 Thread Matthew Aznoe



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
[EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]


Re: [PHP] Variable name declarations?

2001-07-13 Thread Zak Greant

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 [EMAIL PROTECTED]
To: php [EMAIL PROTECTED]
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
 [EMAIL PROTECTED]








 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] Variable name declarations?

2001-07-13 Thread scott [gts]



 -Original Message-
 From: Matthew Aznoe [mailto:[EMAIL PROTECTED]]
 Subject: [PHP] Variable name declarations?
 
 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.

you are basically describing the use strict; pragma of perl.

if you declare use strict; at the beginning of a perl script,
you must then declare all variables before using them (among
other things that use strict prohibits, but variable declaration
is by far the most noticeable effect of use strict)

my $num = 5;#good

$num = 5;   #error

but the great thing is, is that you can turn strict mode off
for specific code blocks if you find a need to get a little
loose with the rules.

no strict;
$x = 5; #no error


i wish PHP had an equivilent to perl's use strict... 
it would certainly save us all a lot of time, and probably
make us clean our code up a little bit :)

 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. 

if PHP make variable declaration an optional thing by use of
something similar to perl's use strict pragma, then it wouldnt
sacrifise anything... becuase only people who wanted it would
use it.

if PHP were to mandate variable declaration for all scripts all
the time, then it would certainly sacrifise a lot... 


 Does anyone else have any thoughts on this? 


yes.  i love the idea... just as long as it's an optional feature
that can be turned on and off as the programmer needs.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Variable name declarations?

2001-07-13 Thread Rasmus Lerdorf

 Does anyone else have any thoughts on this?

error_reporting = E_ALL

in your php.ini file and PHP will warn you whenever you use an unitialized
variable.

-Rasmus


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]