Edit report at https://bugs.php.net/bug.php?id=46504&edit=1

 ID:                 46504
 Comment by:         umassthrower at gmail dot com
 Reported by:        glideraerobatics at hotmail dot com
 Summary:            Lexical variable scope
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 PHP Version:        5.3.0alpha2
 Block user comment: N
 Private report:     N

 New Comment:

This is a no-brainer guys.


Previous Comments:
------------------------------------------------------------------------
[2008-11-06 13:43:19] glideraerobatics at hotmail dot com

Description:
------------
Many languages such as Perl, Java, C++ support lexically scoped variables. 
Lexical scope allows a variable to exist only within its containing closure. 
Using lexically scoped (and therefore also declared) variables prevents a whole 
lot of accidental bugs, memory waste, and poor code design.

Perl has the "my" declaration to achieve this:
http://www.developer.com/lang/perl/article.phpr/3323421

There will have to be some kind of pragma to turn on this feature only in 
functions or files where it's wanted.

Reproduce code:
---------------
my $person = new Person("Joe");

if ($person->isHomeless()) {
  my $house = new House();
  while (!$house->isComplete()) {
    $house->addBricks();
  }
  $person->assignHome($house);
}

// $house no longer exists here.

if ($person->isHungry()) {
  my $food = array('bannanas', 'chocolate');
  $person->eat($food);
}

// $food no longer exists here.





------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=46504&edit=1

Reply via email to