Need help Singleton

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!

Moderator: General Moderators

Post Reply
vichiq
Forum Newbie
Posts: 6
Joined: Tue Jul 28, 2009 12:35 pm

Need help Singleton

Post 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..
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Need help Singleton

Post 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;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need help Singleton

Post by pickle »

What's the error?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Need help Singleton

Post 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
vichiq
Forum Newbie
Posts: 6
Joined: Tue Jul 28, 2009 12:35 pm

Re: Need help Singleton

Post by vichiq »

so what should i use then? 8O
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Need help Singleton

Post 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
vichiq
Forum Newbie
Posts: 6
Joined: Tue Jul 28, 2009 12:35 pm

Re: Need help Singleton

Post 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?
vichiq
Forum Newbie
Posts: 6
Joined: Tue Jul 28, 2009 12:35 pm

Re: Need help Singleton

Post by vichiq »

up, need reply.. wew..
Post Reply