Jump to content


Photo

Can't return user class variables via getter

users class variables setters and getters

Best Answer jbonnett, 18 June 2014 - 01:23 AM

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;
}
Go to the full post


  • Please log in to reply
4 replies to this topic

#1 jbonnett

jbonnett
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationAberystwyth, Wales

Posted 18 June 2014 - 12:08 AM

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;
	}
	
};

?>


#2 Strider64

Strider64
  • Members
  • PipPipPip
  • Advanced Member
  • 247 posts
  • LocationA burb of Detroit, MI

Posted 18 June 2014 - 12:41 AM

<?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();

 http://www.pepster.com  ||  "Insanity: doing the same thing over and over again and expecting different results." - Albert Einstein 


#3 Strider64

Strider64
  • Members
  • PipPipPip
  • Advanced Member
  • 247 posts
  • LocationA burb of Detroit, MI

Posted 18 June 2014 - 12:48 AM

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.


 http://www.pepster.com  ||  "Insanity: doing the same thing over and over again and expecting different results." - Albert Einstein 


#4 jbonnett

jbonnett
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationAberystwyth, Wales

Posted 18 June 2014 - 12:59 AM

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 by jbonnett, 18 June 2014 - 01:03 AM.


#5 jbonnett

jbonnett
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts
  • LocationAberystwyth, Wales

Posted 18 June 2014 - 01:23 AM   Best Answer

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;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users