Jump to content

Really strange scope problem - love some input.


rwboyer

Recommended Posts

Is this a bug???

 

This has nothing to do with wordpress but the example I am using happens to be wordpress where I ran into it lately.

 

Here is the issue:

 

A required/included php file ( let's call it x) there is a global object variable that is instantiated - let's call it n  in another file that is required/included (let's call it y) there is a global definition that is also included/required (let's the file z).

 

so ...

 

file x is included and instantiates global object

file y is included down the line and includes file z that declares n global.

 

The issue is this works in one circumstance and does not work in another.

 

Circumstance 1:

 

$n is actually instantiated

global $n is actually available in file z right after it is declared global

global $n is actually available in file y after inclusion

$GLOBAL['n'] is obviously that object, everything works fine.

 

Circumstance 2:

 

$n is actually instantiated

global $n is actually available in file z right after it is declared global

global $n is null in file y after include of file z.

$GLOBAL['n'] is that object, everything not fine.

 

the differences in circumstance 1 and 2 are:

 

circumstance 1

after $n is instantiated file y is included that calls a function that require_once's file z

 

circumstance 2

after $n is instantiated a function is called that require_once's file y and then the exact same sequence as in circumstance 1 require_once's file z

 

before you ask here is the exact call stacks that causes $n not to be visible vs not visible in file y

 

works fine $n is visible in stack frame 0 and 4

it is declared global in stack frame 0 instantiated way up the stack:

 

0  require_once()  /var/www/wp-content/themes/ninjamonkeys/header.php:14

1  load_template() /var/www/wp-includes/theme.php:822

2  locate_template() /var/www/wp-includes/theme.php:800

3  get_header()    /var/www/wp-includes/general-template.php:34

4  include()      /var/www/wp-content/themes/ninjamonkeys/archive.php:1

5  require_once()  /var/www/wp-includes/template-loader.php:52

6  require()      /var/www/wp-blog-header.php:16

7  {main}          /var/www/index.php:17

 

Does not work $n IS VISABLE in stack frame 0 but NOT in stack frame 4 but.... $GLOBALS['n'] is fine in stack frame 4.

 

0  require_once()  /var/www/wp-content/themes/ninjamonkeys/header.php:14

1  load_template() /var/www/wp-includes/theme.php:822

2  locate_template() /var/www/wp-includes/theme.php:800

3  get_header()    /var/www/wp-includes/general-template.php:34

4  require_once()  /var/www/wp-content/themes/ninjamonkeys/archive.php:1

5  load_template() /var/www/wp-includes/theme.php:822

6  series_includeTemplate() /var/www/wp-content/plugins/organize-series/series-utility.php:94

7  call_user_func_array() /var/www/wp-includes/plugin.php:0

8  do_action()    /var/www/wp-includes/plugin.php:339

9  require_once()  /var/www/wp-includes/template-loader.php:7

10 require()      /var/www/wp-blog-header.php:16

11 {main}          /var/www/index.php:17

 

Note that the call sequence from stack frame 4 down to 0 are the exactly the same in both cases and $GLOBAL['n'] is exactly the same in both cases - the only difference is that in the second circumstance after the global $n is included it is not visible up the stack frame.

 

Sorry if this is not clear but would love some commentary on why this is considering the object exists in both cases and the call/include sequence where the issue actually shows up is exactly the same??????

 

tia

 

RB

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.