Global variable is NULL in class method
<p>My first line in my script I have:</p>

<pre><code>$db = new db_class();
</code></pre>

<p>This is just an example to start the db object. Then I have:</p>

<pre><code>class main {

    function init() {
        $this-&gt;session_start();
    }

    protected function session_start() {
        $sh = new session_handler();
        session_set_save_handler(
            array (&amp; $sh, 'open'),
            array (&amp; $sh, 'close'),
            array (&amp; $sh, 'read'),
            array (&amp; $sh, 'write'),
            array (&amp; $sh, 'destroy'),
            array (&amp; $sh, 'gc')
        );        
    }

}
</code></pre>

<p>All of the problems are in the in <code>session_handler</code> class. This code:</p>

<pre><code>public function write($id, $data) {
    global $db;  

    var_dump($db); //returns NULL
}
</code></pre>

<p>says that <code>$db</code> is <code>NULL</code> instead of an instance of <code>db_class</code>.</p>

<p>Note, <code>db_class</code> objects work except when calling the <code>write()</code> method:</p>

<pre><code>class main {

    function init() {
        global $db;

        var_dump($db); //returns the db object correctly

        $this-&gt;session_start();
    }

    protected function session_start() {
        $sh = new session_handler();
        session_set_save_handler(
            array (&amp; $sh, 'open'),
            array (&amp; $sh, 'close'),
            array (&amp; $sh, 'read'),
            array (&amp; $sh, 'write'),
            array (&amp; $sh, 'destroy'),
            array (&amp; $sh, 'gc')
        );  
    }

}
</code></pre>
