Jump to content

Issue implementing class


supertrucker

Recommended Posts

Hello,I have written a custom class that is going to handle all of my CRUD operations for my "user" table. Below is the class:

<?php 	class UserDb {			const TABLE = "users";		const KEY_ID = "Id";		const KEY_EMAIL = "email";		const KEY_JOIN_DATE = "joinDate";		const KEY_VERIFIED = "verified";		const KEY_LAST_LOGIN = "lastLogin";		const KEY_FIRST_NAME = "firstName";		const KEY_LAST_NAME = "lastName";		const KEY_PASSPHRASE = "passphrase";				private $db = null;		public function __construct() {			$this->db = new PDO(					'mysql:host=' . HOST_NAME . 					';dbname=' . DATABASE_NAME . 					';charset=utf8', DATABASE_USER, DATABASE_PASS			);			$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);		}			public function userExists($email) {			$sql = $this->db->prepare("SELECT * FROM " . self::TABLE . " WHERE " . self::KEY_EMAIL . " = :email");			$sql->execute(array(':email' => $email));			$row_count = $sql->rowCount();			if ($row_count > 0) { return true; }			else { return false; }		}				public function insertUser($user = null) {			if (is_null($user)) { $user = new User(); } 		}			}	?>

When I run this, I get:

Fatal error: Using $this when not in object context in /media/mypassport/var/www/jcbook/include/classes/class.UserDb.php on line 28
Can anyone tell me what I've done wrong? I've never worked with classes in php before.Thank you in advance! Edited by supertrucker
Link to comment
Share on other sites

Wow, I feel a little dumb now! I was calling the method with '::' instead of '->', thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...