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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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]; } Quote Link to comment 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. Quote Link to comment 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" ); ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 Could I use a global? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
unidox Posted July 15, 2010 Author Share Posted July 15, 2010 What do you mean by floored? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 I'd guess he means flawed. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.