Page 1 of 1

Need help Singleton

Posted: Tue Jul 28, 2009 12:49 pm
by vichiq
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:

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>
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..

Re: Need help Singleton

Posted: Tue Jul 28, 2009 3:05 pm
by tr0gd0rr
This may not be the problem, but I've always used "new self()" instead of __CLASS__:

Code: Select all

public static function getInstance(){
  if(!isset(self::$instance)){
    self::$instance = new self();
  }
  return self::$instance;
}

Re: Need help Singleton

Posted: Tue Jul 28, 2009 3:30 pm
by pickle
What's the error?

Re: Need help Singleton

Posted: Tue Jul 28, 2009 4:18 pm
by Eran
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

Re: Need help Singleton

Posted: Tue Jul 28, 2009 5:23 pm
by vichiq
so what should i use then? 8O

Re: Need help Singleton

Posted: Tue Jul 28, 2009 5:39 pm
by Eran
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:

Code: Select all

 
    public static function setValues($userIn, $passIn, $firstIn, $lastIn, $midIn){
          $self = self::getInstance();
          $self->$username = $userIn;
          $self->$password = $passIn;
          $self->$first_name = $firstIn;
          $self->$last_name = $lastIn;
          $self->$middle_name = $midIn;
    }
$self is just a local name I gave the singleton instance. It has no special meaning, unlike $this

Re: Need help Singleton

Posted: Tue Jul 28, 2009 5:45 pm
by vichiq
hi guy i edited the code again.

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 function setValues($userIn, $passIn, $firstIn, $lastIn, $midIn){
         echo"<br> setting values $userIn; $passIn; $firstIn; $lastIn; $midIn";
         $username = $userIn;
         $password = $passIn;
         $first_name = $firstIn;
         $last_name = $lastIn;
         $middle_name = $midIn;
         echo "<br> receiving values $username;$password;$first_name;$last_name;$middle_name";
     }
 
     public function reportuser(){
         echo "<br> receiving values $username;$password;$first_name;$last_name;$middle_name";
     }
 
     public static function getInstance(){
         if(!isset(self::$instance)){
             self::$instance = new userInformation();
         }
         return self::$instance;
     }
}
?>
 
 

Code: Select all

 
<!--
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:


setting values s; s; s; s; s
receiving values s;s;s;s;s
receiving values ;;;;

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?

Re: Need help Singleton

Posted: Tue Jul 28, 2009 8:16 pm
by vichiq
up, need reply.. wew..