unidox Posted July 14, 2010 Share Posted July 14, 2010 I have just started picking up PHP again, so my OOP skills are a bit rusty. I keep getting this error: Fatal error: Using $this when not in object context in C:\wamp\www\core\dbClass.php on line 93 That line is: $this->last_query = "Table: " . $table . "Query: " . $sql; I am calling an sql function inside dbClass from userClass: $query = dbClass::sql ( "users", "WHERE `id` = '$userId'" ); Any ideas what I am doing wrong? Thanks Link to comment https://forums.phpfreaks.com/topic/207777-error/ Share on other sites More sharing options...
trq Posted July 14, 2010 Share Posted July 14, 2010 The sql method is static, you cannot use $this within a static method because you do not have an object to refer to. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086150 Share on other sites More sharing options...
unidox Posted July 14, 2010 Author Share Posted July 14, 2010 Ok, so I can do two things to my understanding: 1) Use $dbClass->sql instead of dbClass::sql 2) Change $this->last_query to $last_query Am I right? Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086151 Share on other sites More sharing options...
trq Posted July 14, 2010 Share Posted July 14, 2010 We would need to see more code. Generally though. Static methods get over used by people who don't know what they are doing / how they work. A database class really needs to be instantiated because it should store at least a little state. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086164 Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 dbClass: <?php class dbClass { private $last_error; private $last_query; private $row_count; public $owner_email; public $db_host; public $db_username; public $db_password; public $db_table; public $site_dir; public $prefix; private $link; function __construct( $host, $username, $password, $table ) { $this->db_host = $host; $this->db_username = $username; $this->db_password = $password; $this->db_table = $table; $this->connect(); } function connect () { $this->link = mysql_connect($this->db_host, $this->db_username, $this->db_password); if (!$this->link) { $this->last_error = mysql_error(); return false; } if (!$this->select_table()) { return false; } return $this->link; } function select_table() { if (!mysql_select_db($this->db_table)) { $this->last_error = mysql_error(); return false; } return true; } function sql($table, $sql) { $this->last_query = "Table: " . $table . "Query: " . $sql; $row = mysql_query("SELECT * FROM `" . $prefix . $table . "` " . $sql . "") or die ( mysql_error ( ) ); if (!$row) { $this->last_error = mysql_error(); return false; } $this->row_count = mysql_num_rows($row); return $row; } function get_row($result, $type="MYSQL_BOTH") { if (!$result) { $this->last_error = "Invalid resource identifier passed to get_row() function."; return false; } if ($type == 'MYSQL_ASSOC') $row = mysql_fetch_array($result, MYSQL_ASSOC); if ($type == 'MYSQL_NUM') $row = mysql_fetch_array($result, MYSQL_NUM); if ($type == 'MYSQL_BOTH') $row = mysql_fetch_array($result, MYSQL_BOTH); if (!$row) return false; return $row; } function insert_sql($table, $sql1, $sql2) { $this->last_query = "Table: " . $table . "Query: " . $sql1 . " | " . $sql2; $row = mysql_query("INSERT INTO `" . $this->prefix . $table . "` (" . $sql1 . ") VALUES (" . $sql2 . ")") or die(mysql_error()); if (!$row) { $this->last_error = mysql_error(); return false; } return true; } function update_sql($table, $sql) { $this->last_query = "Table: " . $table . "Query: " . $sql; $row = mysql_query("UPDATE `" . $this->prefix . $table . "` " . $sql . ""); if (!$row) { $this->last_error = mysql_error(); return false; } return true; } function escape_sql($data) { return mysql_real_escape_string($data); } function print_last_error() { return $this->last_error; } function print_last_query() { return $this->last_query; } } ?> Function being used inside userClass: public function getData ( $userId, $rowName ) { $query = dbClass::sql ( "users", "WHERE `id` = '$userId'" ); if ( mysql_num_rows( $query ) == 1 ) { $row = dbClass::get_row( $query ); } else { return "Error: No rows in query."; } return $row[$rowName]; } Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086169 Share on other sites More sharing options...
trq Posted July 15, 2010 Share Posted July 15, 2010 Pfff. You haven't even declared any methods within the dbClass as static. This.... $query = dbClass::sql ( "users", "WHERE `id` = '$userId'" ); should be.... $db = new dbClass($host, $username, $password, $table); $query = $db->sql("users", "WHERE `id` = '$userId'" ); ps: The $table variable is misleading, it should be $dbname or similar. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086172 Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 Well, the db is already declared inside index.php: <?php session_start( ); // exit ( $_SESSION['userId'] ); include ("../config.php"); include ("../core/coreClass.php"); include ("../core/dbClass.php"); include ("../core/userClass.php"); define (IN_SCRIPT, true); $coreClass = new coreClass ( ); $dbClass = new dbClass ( $host, $username, $password, $table ); $userClass = new userClass ( ); if ( !$userClass->checkLoggedIn( ) ) { $currentPage = "login"; } elseif ( $coreClass->getGET("page") && file_exists ( "pages/".$coreClass->getGET("page").".php" ) ) { $currentPage = $coreClass->getGET("page"); } else { $currentPage = "home"; } include ( "processors/".$currentPage.".php" ); include ( "pages/".$currentPage.".php" ); ?> Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086174 Share on other sites More sharing options...
trq Posted July 15, 2010 Share Posted July 15, 2010 Still, it won't exists within the userClass. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086176 Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 Could I use a global? Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086177 Share on other sites More sharing options...
trq Posted July 15, 2010 Share Posted July 15, 2010 Could I use a global? That would break any encapsulation you may have. Your design is pretty floored anyways though. Ideally, you would pass your database object (ensuring it implements some interface) to the construct of the userClass. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086180 Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 What do you mean by floored? Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086183 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 I'd guess he means flawed. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086190 Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 Can I ask how I can improve it? Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086192 Share on other sites More sharing options...
trq Posted July 15, 2010 Share Posted July 15, 2010 Can I ask how I can improve it? One thing would be to read my last reply. Link to comment https://forums.phpfreaks.com/topic/207777-error/#findComment-1086213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.