PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi im new with php and ive been trying to implement the singleton design pattern but i got an error and i dont know what seems wrong, since i tried the class when it wasn't in singleton and it worked fine. hope you can help me. here's the code:
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of userInformation
*
* @author mettere`avanti
*/
class userInformation {
var $username;
var $password;
var $first_name;
var $last_name;
var $middle_name;
private static $instance ;
private function __construct()
{
}
public static function setValues($userIn, $passIn, $firstIn, $lastIn, $midIn){
$this->$username = $userIn;
$this->$password = $passIn;
$this->$first_name = $firstIn;
$this->$last_name = $lastIn;
$this->$middle_name = $midIn;
}
public static function getInstance(){
if(!isset(self::$instance)){
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
//put your code here
}
?>
The problem is that you can't use the $this scope in a static method. A static method belongs to the class not to a specific object, which $this represents
Take a look at your setValues() method - you need to replace $this with the singleton instance. This should be easy since you already have the method that retrieves it:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php include 'userInformation.php';?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Validating Log in</title>
</head>
<body>
<?php
$myuserInstance = userInformation::getInstance();
$myuserInstance->setValues("s","s","s","s","s");
$myuserInstance->reportuser();
?>
</body>
</html>
the problem is when i run uservalidation.php it says:
question is why doesn't reportuser() function report the same output as that from earlier, i didn't re set the values of the variables anyway. what am i getting wrong?