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')
        );  
    }

}
11
  • 1
    What it says when you var_dump just below this code of line $db = new $db_class(); ? Commented Feb 4, 2012 at 9:43
  • 2
    Why you use the globals, it would be much better if you pass in the db instance into class. Commented Feb 4, 2012 at 9:44
  • $db=new $db_class() should be $db= new db_class() shoudn't it? Commented Feb 4, 2012 at 9:44
  • 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 Commented Feb 4, 2012 at 9:50
  • 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 stackoverflow.com/questions/5166087/php-global-in-functions/… Commented Feb 4, 2012 at 10:02

1 Answer 1

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);
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

no its not the problem because this is just example its started and work anywhere
this is not the problem i'm sure

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.