Req #69392 [NEW]: Local variable scope in included files

From: Date: Tue, 07 Apr 2015 10:28:28 +0000
Subject: Req #69392 [NEW]: Local variable scope in included files
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191870@lists.php.net to get a copy of this message
From:             gaborca at webvision dot hu
Operating system: Any
PHP version:      Irrelevant
Package:          Variables related
Bug Type:         Feature/Change Request
Bug description:Local variable scope in included files

Description:
------------
Like static variables in C, there could be variables in PHP that are
only locally available in an included file, but not available from
outside.

This way local variables would work like private properties in objects,
but without the need of using all the ceremony of OOP.

I'm not sure yet about the need of using the local keyword in functions
defined in the included file.

Test script:
---------------
child.php
<?php
  local $local_variable = 'a local variable in included module';
  function local_function() {
    print "Local variables are visible in local functions
({$local_variable}).";
  }
  print "Our local variable is '{$local_variable}'.";
  local_function();
?>

mother.php
<?php
  include('child.php');
  print "child.php's local variable is '{$local_variable}'.";
?>

Expected result:
----------------
Our local variable is 'a local variable in included module'.
Local variables are visible in local functions (a local variable in
included module).
child.php's local variable is ''.
Notice: Undefined variable: local_variable in mother.php on line 3

Actual result:
--------------
Our local variable is 'a local variable in included module'.
Notice: Undefined variable: local_variable in child.php on line 4
Local variables are visible in local functions ().
child.php's local variable is 'a local variable in included module'.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=69392&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=69392&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=69392&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=69392&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=69392&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=69392&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=69392&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=69392&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=69392&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=69392&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=69392&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=69392&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=69392&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=69392&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69392&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=69392&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=69392&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=69392&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69392&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=69392&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=69392&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#191870) next »