PHP: is there any case in which "include_once" resets the stack?

69 views Asked by At

Say I have this code:

class control {
  function public_home()
    {
        $pagina=$this->load_public_page();
        ...
        echo $pagina;
    }

...
        function load_public_page($localnode="") {

            global $language;

            if ($localnode != "") { $localnode="_" . $localnode; }

            $langlink = "http://" . PAGE_INDEX;
            if ($language != "es") { $langlink = $langlink . "/" . $language . "/"; }

            ob_start();
            include_once 'page.php';
            include_once 'header.php';
            $pagina=ob_get_contents();
            ob_end_clean();
            return $pagina;
            }
...
}

And then, in page.php:

<?php
echo "LANG " . $langlink;
echo "NODE " . $localnode;
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...

page.php should be able to access both $langlink and $localnode variables without problems, right? (I have tried with a simplified example and it does work). Well, in my case it doesn't; no matter how I try, the code in page.php says that $langlink and $localnode are undefined.

Not only that: if I add a var_dump(debug_backtrace()) to page.php, it will show me only:

array(1) {
  [0]=>
  array(3) {
    ["file"]=>
    string(53) "index.php"
    ["line"]=>
    int(55)
    ["function"]=>
    string(12) "include_once"
  }
}

In other words, it doesn't pick up neither the load_public_page function, not its containing class... No wonder it can't find those variables.

It's extremely odd, because look at the simplified example I mentioned above:

class Ble {
    function bla() {
        $a=127;

        $pagina=$this->otrosi();
        echo $pagina;
        }

    function otrosi($bbb="") {

        $a=666;

        ob_start();
        include_once('b.php');
        $pagina=ob_get_contents();
        ob_end_clean();

        return $pagina;
        }
    }

$g=new Ble();   
$g->bla();

Where b.php is:

<p>BBB is: <?php echo $bbb; ?></p>

<p>What is A? <?php echo $a; ?>

It does print $a as 666, as it should, and of course, if I add a debug_backtrace in b.php, it shows me the full stack trace: object Ble, function bla, function otrosi....

As the title says: does anyone know of any circunstances in which an include_once or output buffering might reset the stack in PHP? I'm a bit desperate, because this is one of those things where, according to docs and simple examples, it SHOULD work... and yet it doesn't.

0

There are 0 answers