php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46504 Lexical variable scope
Submitted: 2008-11-06 13:43 UTC Modified: 2015-06-22 08:31 UTC
Votes:27
Avg. Score:4.3 ± 1.1
Reproduced:23 of 24 (95.8%)
Same Version:8 (34.8%)
Same OS:5 (21.7%)
From: glideraerobatics at hotmail dot com Assigned: kalle (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.0alpha2 OS:
Private report: No CVE-ID: None
 [2008-11-06 13:43 UTC] 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.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-04 00:28 UTC] umassthrower at gmail dot com
This is a no-brainer guys.
 [2015-06-22 08:31 UTC] kalle@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: kalle
 [2015-06-22 08:31 UTC] kalle@php.net
I think you should make an RFC on the wiki and post it to the mailing list for some reviews instead for some more discussion.

Personally I would be in favour of such, although I would prefer another keyword like "local" instead of "my".

See http://wiki.php.net/ for more information
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Apr 21 16:01:25 2019 UTC