Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP 5 Class instantiation

Status
Not open for further replies.

chigley

Programmer
Sep 30, 2002
104
GB
This is a newbie question.

I have created a class to handle user accounts. The code (as far as I have got) is shown below.

<?
class User{

private $title;
private $firstName;
private $initials;
private $surname;
private $userName;
private $pwd;
private $accountStatus;
private $securityLevel;

public function setUserName ($newUserName){
%this->userName = $newUserName;
}
public function getUserName (){
return $this->userName;
}

public function __construct($newUserName) {

$this->$setUserName = $newUserName;
}


public function validatePassword ($checkPassword){

if ($checkPassword == $this->pwd) {
return true;
}
else
{
return false;
}
}

public function updateUserDetails (){
}

public function suspendUserAccount(){

$this->accountStatus="S";
$this->updateUserDetails();

}

public function reinstateUserAccount(){
$this->accountStatus="A";
$this->updateUserDetails();
}

public function deactivateUserAccount(){
$this->accountStatus="D";
$this->updateUserDetails();
}

}
?>


My test class throws no errors, yet does nothing. What am I doing wrong? The faults clearly must lie in the User class.

The test class code is as follows :

<?
/*PHP Test Page to test the classes*/
include "User.php";

$newUser = new User("BEX");
echo "<br>";
echo $newUser->getUserName;
echo "Hello World";

?>

I get no output whatsoever!! If I comment out the lines that instantiate the User class, and call the getUserName method then I get "Hello World" in the browser.
 
I turned on PHP debugging and found the problem was on line 18 %this instead of $this. Gradually getting the hang of this open source malarky!
 
If you reread the questions you will find that the only thing that is the same is the code. One question deals with debugging an instantiation of an object, the other with calling a method on that object. These are TWO different questions, ableit from the same code base.
 
i assume that sleipnir214's answer to your other thread also solved this one.

this line
Code:
echo $newUser->getUserName;
is incorrect as you are trying to address a property which does not exist.
it looks like you are trying to address a method which should be done thus:
Code:
echo $newUser->getUserName();

but before you fix this bit, your class is a bit flawed

line 14 - change the % for a $
line 22 - change to
Code:
 $this->setUserName($newUserName);

there may be other errors that i have not spotted.

it's clear that you have error display turned off or many of these issues will have been apparent to you. make the switch in your php.ini on your development box.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top