2

According to the PHP manual $a should be available to b.inc in the following code segment:

<?php
$a = 1;
include 'b.inc';
?>

However when I try to do the same when calling a static method, $a seems to be out of scope.

class foo {
    public static function bar() {
        $a = 1;
        include('b.inc');               
    }
}

foo::bar();

Am I misunderstanding something? EDIT: Okay, I'm an idiot, everybody. I was using a wrapper function for the include -- to fill in the include path. This took the variable out of scope. Thanks for the help.

3
  • what is the contents of b.inc Commented Sep 10, 2010 at 17:30
  • let's say "echo $a;" for simplicity's sake. Commented Sep 10, 2010 at 17:31
  • You can also delete this question. There is also a SO badge for cleaning up ;) Commented Mar 16, 2022 at 17:24

4 Answers 4

1

It totally works for me. If you are describing the problem accurately, something must be happening to $a inside of b.inc.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you -- I must have screwed up somewhere. Apologies.
Yes. I also have my include in a foreach loop -- but I don't see how that would make any difference. I'll post my error when as soon as I figure it out.
Man! No offense, Greg, but I seriously question your PHP proficiency if you are including a file in a loop.
It's just an HTML template to avoid putting a bunch of markup into the helper function. Is there a serious problem with this?
Okay -- even though this might not see a whole lot of iterations, I suppose that is more than just a little herp derp on my part. The loop is in the template itself template now.
|
1

Function scope is different than the global scope. b.inc will see $a, and any variables created in b.inc will be in foo::bar()'s scope (unless they are defined as globals, or inside their own function scope).

You can test this with some code:

function foo() {
    $a = 1;
    include '1.php'; // modify and initialize, ie. $a++; $b = 3; 
    include '2.php'; // test the values: $a == 2; $b == 3
}

We run into this problem occasionally when we bootstrap WordPress in other scripts: the initialization scripts assume they are in the outermost scope so they will set variables like $wpdb (the database object), but that file will actually get created in some function that did a require_once(). The solution is to always bootstrap WordPress in the app before you get into any function scope.

Comments

0

Inside the class you need to say that you mean the global one.


class foo {
    public static function bar() {
        global $a;
        var_dump($a);
    }
}

foo::bar();

1 Comment

You misunderstood his question. He's saying that when he includes it in a static function, the file does not seem to see $a.
0
<?php
//a.php
class test
{
    public static function run()
    {
        $a = 'Some value';
        include 'b.php'; //echo $a;
    }
}
test::run();
?>

all works fine in my test aswell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.