Jump to content
Sign in to follow this  
jbonnett

Can't return user class variables via getter

Recommended Posts

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

?>

Share this post


Link to post
Share on other sites


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

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.