How comes inisialisasi variable?

0 like 0 dislike
Interesting situation, have something like a small template engine.
Here is a small clipping from the code:
class Decor { public function draw() { ob_start(); $this->loadThemePart('name'); $buffer = ob_get_contents(); ob_end_clean(); ... return $buffer; } public function loadThemePart($template_name) { if(!isset($some_var)) { $some_var = 'test variable'; } $expansionFile = '/some/path/to/file.php'; include_once $expansionFile; include_once $template_name; } }

And now, if you write this example in $expansionFile:
var_dump($some_var); function old() { global $some_var; var_dump($some_var); }

in the first case will test variable, and the second NULL
Can not understand why is this happening?
asked by | 14 views

2 Answers

0 like 0 dislike
An interesting situation

Nothing interesting, just need to figure out how to work the scope.

LoadThemePart inside a method you declare a new variable $some_var, its scope is limited by this method, i.e., outside this method, it is impossible to access.
Then you plug in a new php file. Because it is connected within the scope of the method loadThemePart, he has access to the local variable named $some_var.
In the old function you are trying to access a global variable $some_var, but it's not declared anywhere in the code, there are local $some_var. Consequently, the global $some_var is null or empty.
answered by
0 like 0 dislike
Variables in classes are isolated from global variables, but since you includes file $expansionFile inside the class, that he is and isolated the variables of this class, you will not tell about the function - it looks at global variables. The function inside a class method global and cannot operate on class variables
answered by

Related questions

0 like 0 dislike
2 answers
asked Aug 11 by iNickolay
0 like 0 dislike
2 answers
asked Jul 4 by iNickolay
0 like 0 dislike
1 answer
110,581 questions
257,150 answers
476 users