Jump to content

Can't return user class variables via getter


jbonnett
Go to solution Solved by jbonnett,

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

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

 

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

  • Solution

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;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.