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: 2021-02-23 16:20 UTC
From: gaborca at webvision dot hu Assigned:
Status: Suspended Package: Variables related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
21 - 3 = ?
Subscribe to this entry?

 
 [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

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-23 16:20 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-02-23 16:20 UTC] cmb@php.net
This feature request requires discussion for what this bug tracker
is not suitable.  Please pursue the RFC process[1].  For the time
being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC