php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69392 Local variable scope in included files
Submitted: 2015-04-07 10:28 UTC Modified: -
From: gaborca at webvision dot hu Assigned:
Status: Open Package: Variables related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gaborca at webvision dot hu
New email:
PHP Version: OS:

 

 [2015-04-07 10:28 UTC] gaborca at webvision dot hu
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'.

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Nov 17 14:01:40 2019 UTC