This question already has an answer here:
Can someone explain how files are included inside methods. Here is what I don't understand: Let's say we have 2 files:
//a_variable.php
$variable=1;
And
//b_variable.php
$variable++;
Upon calling call_variable() from:
class TestView
{
public static function a_variable()
{
require 'View/test/a_variable.php';
}
public static function b_variable()
{
require 'View/test/b_variable.php';
}
public static function call_variable()
{
self::a_variable();
self::b_variable(); //doesn't know $variable. As expected.
}
}
I get "Undefined variable: variable", which is expected, because $variable has only local scope inside the a_variable() method.
But, exactly the same code, using methods:
//a_method.php
function method()
{
echo "a";
}
And
//b_method.php
function method()
{
echo "b";
}
Upon calling call_method() from:
class TestView
{
public static function a_method()
{
require 'View/test/a_method.php';
}
public static function b_method()
{
require 'View/test/b_method.php';
}
public static function call_method()
{
self::a_method();
self::b_method(); //DOES know method(). Why?
}
}
I get "Fatal error: Cannot redeclare method() (previously declared in View\test\a_method.php:4) in View\test\b_method.php on line 5"
Why is the name of the included method visible outside the scope of the caller method?
</div>