0

My first line in my script I have:

$db = new db_class();

This is just an example to start the db object. Then I have:

class main {

    function init() {
        $this->session_start();
    }

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

}

All of the problems are in the in session_handler class. This code:

public function write($id, $data) {
    global $db;  

    var_dump($db); //returns NULL
}

says that $db is NULL instead of an instance of db_class.

Note, db_class objects work except when calling the write() method:

class main {

    function init() {
        global $db;

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

        $this->session_start();
    }

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

}
Jared Farrish
  • 46,034
  • 16
  • 88
  • 98
Mac Os
  • 97
  • 1
  • 6
  • 1
    What it says when you var_dump just below this code of line `$db = new $db_class(); `? – Shakti Singh Feb 04 '12 at 09:43
  • 2
    Why you use the globals, it would be much better if you pass in the db instance into class. – Risto Novik Feb 04 '12 at 09:44
  • $db=new $db_class() should be $db= new db_class() shoudn't it? – SoWhat Feb 04 '12 at 09:44
  • the db object, its working any where else – Mac Os Feb 04 '12 at 09:45
  • @jurka i cant use php version > 5.2 – Mac Os Feb 04 '12 at 09:46
  • @Somesh Mukherjee i said this is just exaple the db object work great in any function as a globals instead of this – Mac Os Feb 04 '12 at 09:47
  • 2
    Is the db class being instantiated when you call the main() class? As already suggested, why not passing the object to the main() class, or instantiating the db class there, instead? You could also go for an autoloading function, I think – Damien Pirsy Feb 04 '12 at 09:50
  • ican't do that because it will cause main - user_class - main - db -feach – Mac Os Feb 04 '12 at 09:55
  • 1
    This question cannot be answered without knowing the full global scope. You or your script is probably overwriting $db at some place. That's why you dont use globals. See http://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527 – Gordon Feb 04 '12 at 10:02
  • I don't know that this question makes any sense. This (minus the `$db_class()`, which the OP indicated was a typo) seems to work: http://codepad.org/pyOtgVJ8 – Jared Farrish Feb 04 '12 at 10:03
  • i'm using php 5.2 and i can not upgrade – Mac Os Feb 04 '12 at 11:58

1 Answers1

1

I guess the problem is at first line
$db = new $db_class();

if guess it should be like

$db = new db_class();

Or make sure that $db_class has value of class name that you wish to initialize

How about trying something like this

class main{
protected $_db;

function init($db){
    $this->_db = $db;
    $this->session_start();
}

protected function session_start() {
    $sh = new session_handler();
    session_set_save_handler(
        array (& $sh, 'open'),
        array (& $sh, 'close'),
        array (& $sh, 'read'),
        array (& $sh, 'write'),
        array (& $sh, 'destroy'),
        array (& $sh, 'gc')
    );        
}
public function write($id, $data) {
    vardump($this->_db);
}
}
Jaspreet Chahal
  • 2,701
  • 1
  • 13
  • 17