Need help Singleton
Posted: Tue Jul 28, 2009 12:49 pm
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:
I used a test phpto try the class
but everytime i run index.php it says
Fatal error: Using $this when not in object context in D:\xampp\htdocs\webLRCMS\userInformation.php on line 25
hope you guys can help..
Code: Select all
<?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
}
?>
I used a test phpto try the class
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php include_once 'userInformation.php';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
?><br>
<?php
$myuser = userInformation::getInstance();
$myuser->setValues("s","s","s","s","s");
echo "$myuser->first_name";
?>
</body>
</html>Fatal error: Using $this when not in object context in D:\xampp\htdocs\webLRCMS\userInformation.php on line 25
hope you guys can help..