Jump to content

Call to a member function _safestring() on a non-object


PHPiSean

Recommended Posts

When I use another class inside one class, I get this error

 

Call to a member function _safestring() on a non-object

 

The code is here

<?php

    require ('db.inc.php');
    $db = new db();
    
class common {
    
    function _cookiecheck() {
        if(!isset($_SESSION['username'])) {
            if(isset($_COOKIE['username'])&&isset($_COOKIE['password'])) {
                $pass = $db->_safestring($_COOKIE['password']);
                $user = $db->_safestring($_COOKIE['username']);
                $check = _query("select password from users where username='{$user}'");
                 if($check) {
                     $_SESSION['username'] = $user;
                 }
            }
        }
    }
    

}

?>

 

I get the error on the line when I call $db->_safestring();

 

Thanks for the help

Use need to define $db inside the class / function scope. The class / function does not know what $db is, as it is out of scope. 

 

Check out http://php.net/manual/en/language.variables.scope.php for more information.

I would suggest reading:

 

http://php.net/manual/en/language.oop5.php

 

To learn where you are going wrong with that. Aside from that, here is probably what you are doing:

 

<?php    
class common {
        require ('db.inc.php');
    $db = new db();

    function _cookiecheck() {

 

 

Here is one way to do it:

 

<?php    
require ('db.inc.php');

class common {
    function _cookiecheck() {
              $db = new db();

 

Which will work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.