PHPiSean Posted November 1, 2011 Share Posted November 1, 2011 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 Link to comment https://forums.phpfreaks.com/topic/250212-call-to-a-member-function-_safestring-on-a-non-object/ Share on other sites More sharing options...
premiso Posted November 1, 2011 Share Posted November 1, 2011 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. Link to comment https://forums.phpfreaks.com/topic/250212-call-to-a-member-function-_safestring-on-a-non-object/#findComment-1283875 Share on other sites More sharing options...
PHPiSean Posted November 1, 2011 Author Share Posted November 1, 2011 When I tried to add require('db.inc.php'); $db = new db(); at the beginning of the class I get syntax error, unexpected T_REQUIRE, expecting T_FUNCTION Link to comment https://forums.phpfreaks.com/topic/250212-call-to-a-member-function-_safestring-on-a-non-object/#findComment-1283877 Share on other sites More sharing options...
premiso Posted November 1, 2011 Share Posted November 1, 2011 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. Link to comment https://forums.phpfreaks.com/topic/250212-call-to-a-member-function-_safestring-on-a-non-object/#findComment-1283885 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.