jbonnett Posted June 18, 2014 Share Posted June 18, 2014 All my code is returning is the username... Please help. index.php <?php include('user.class'); $user = new user("Jbonnett", "0", "Admin", "Jamie", "Bonnett", "jbonnett@site.co.uk", "01/09/1992"); echo "username: " . $user->getUsername() . "<br/>"; echo "id: " . $user->getId() . "<br/>"; echo "level: " . $user->getLevel() . "<br/>"; echo "Forename: " . $user->getForename() . "<br/>"; echo "Surname: " . $user->getSurname() . "<br/>"; echo "Email: " . $user->getEmail() . "<br/>"; echo "Dob: " . $user->getDob() . "<br/>"; ?> user.class <?php class user { private $username; private $id; private $level; private $forename; private $surname; private $email; private $dob; public function user($username, $id, $level, $forname, $surname, $email, $dob) { $this->setUsername($username); $this->setId($id); $this->setLevel($level); $this->setForename($forename); $this->setSurname($surname); $this->setEmail($email); $this->setDob($dob); } public function destroy() { unset($this->username); unset($this->id); unset($this->level); unset($this->forename); unset($this->surname); unset($this->uemail); unset($this->dob); } public function setUsername($username) { $this->username = $username; } public function getUsername() { return $this->username; } public function setId($id) { $this->id = $id; } public function getId() { return $this->$id; } public function setLevel($level) { $this->level = $level; } public function getLevel() { return $this->level; } public function setForename($forename) { $this->foreName = $forename; } public function getForename() { return $this->forename; } public function setSurname($surname) { $this->surName = $surname; } public function getSurname() { return $this->surname; } public function setEmail($email) { $this->email = $email; } public function getEmail() { return $this->email; } public function setDob($dob) { $this->dob = $dob; } public function getDob() { return $this->dob; } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/289191-cant-return-user-class-variables-via-getter/ Share on other sites More sharing options...
Strider64 Posted June 18, 2014 Share Posted June 18, 2014 <?php class user { private $username; private $id; private $level; private $forename; private $surname; private $email; private $dob; public function user($username, $id, $level, $forname, $surname, $email, $dob) { $this->username = $username; $this->id = $id; $this->level = $level; $this->forename = $forname; $this->surname = $surname; $this->email = $email; $this->dob = $dob; } public function destroy() { unset($this->username); unset($this->id); unset($this->level); unset($this->forename); unset($this->surname); unset($this->uemail); unset($this->dob); } public function setUsername($username) { $this->username = $username; } public function getUsername() { return $this->username; } public function setId($id) { $this->id = $id; } public function getId() { return $this->$id; } public function setLevel($level) { $this->level = $level; } public function getLevel() { return $this->level; } public function setForename($forename) { $this->foreName = $forename; } public function getForename() { return $this->forename; } public function setSurname($surname) { $this->surName = $surname; } public function getSurname() { return $this->surname; } public function setEmail($email) { $this->email = $email; } public function getEmail() { return $this->email; } public function setDob($dob) { $this->dob = $dob; } public function getDob() { return $this->dob; } }; $user = new user("Jbonnett", "0", "Admin", "Jamie", "Bonnett", "jbonnett@site.co.uk", "01/09/1992"); echo $user->getEmail(); echo '<br>'; echo $user->getLevel(); Quote Link to comment https://forums.phpfreaks.com/topic/289191-cant-return-user-class-variables-via-getter/#findComment-1482811 Share on other sites More sharing options...
Strider64 Posted June 18, 2014 Share Posted June 18, 2014 To pull it from a database table is even easier <?php # User class - Store user info and functions to access/control the flow of data. class User { // The member attributes containing required and optional information. // The attributes must correspond to the database table columns: private $id = NULL; private $userType = NULL; // Required (assigned enum) private $username = NULL; // Required private $email = NULL; // Required private $pass = NULL; // Required private $fullName = NULL; private $validation_code = NULL; private $address = NULL; private $city = NULL; private $state = NULL; private $zipCode = NULL; // Method returns the user ID: public function getId() { return $this->id; } // Grab the user's username: public function getUsername() { return $this->username; } // Grab the user's full name: public function getFullName() { return $this->fullName; } // Grab the password: public function getPass() { return $this->pass; } public function getUserType() { return $this->userType; } // Clear the password once user is logged in: public function clearPass() { $this->pass = NULL; } public function getEmail() { return $this->email; } // Method returns a Boolean if the user is an administrator: public function isAdmin() { return ($this->userType == 'author'); } public function isSysop() { return ($this->userType == 'sysop'); } public function isNewUser() { return ($this->userType == 'public'); } // Method returns a Boolean indicating if the user is an administrator // or if the user is the original author of the provided page: public function canEditPage(Page $page) { return (($this->isAdmin() && ($this->id == $page->getCreatorId()))); } // Method returns a Boolean indicating if the user is an administrator or an author: public function canCreatePage() { return ($this->isAdmin() || $this->isSysop()); } } Then you could do something like: // Validate the form data: if (isset($_POST['action']) && $_POST['action'] == 'loginUser') { // Check against the database: $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username'; $stmt = $pdo->prepare($query); $stmt->execute(array(':username' => $_POST['username'])); $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $stored_user_data = $stmt->fetch(); // More code goes here..... I pulled this from one of my scripts. Quote Link to comment https://forums.phpfreaks.com/topic/289191-cant-return-user-class-variables-via-getter/#findComment-1482812 Share on other sites More sharing options...
jbonnett Posted June 18, 2014 Author Share Posted June 18, 2014 (edited) Apparently that doesn't work for me either... Do you have to initialize the class in the same file defining the strings? Or can I still keep them in the index.php file? And the Setters should work inside the constructor... Even the intellisense provides the example to do it in dreamweaver. Edited June 18, 2014 by jbonnett Quote Link to comment https://forums.phpfreaks.com/topic/289191-cant-return-user-class-variables-via-getter/#findComment-1482813 Share on other sites More sharing options...
Solution jbonnett Posted June 18, 2014 Author Solution Share Posted June 18, 2014 I found the problem... All my code was correct apart from: public function getId() { return $this->$id; } I just needed to change the one line to: public function getId() { return $this->id; } Quote Link to comment https://forums.phpfreaks.com/topic/289191-cant-return-user-class-variables-via-getter/#findComment-1482814 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.