lundi 3 décembre 2018

singleton pattern php issue

i tried to make an object from user class and use the same object in two files, one file to set data and the other get the data, so i tried singleton pattern ,but it doesn't work

User class

<?php 

class user{
private $username;
private $password;
private static $instance;
private function __construct(){

}
protected function __clone()
{
    //Me not like clones! Me smash clones!
}

public static function getInstance(){
     if(null === self::$instance){
         self::$instance = new self();
     }
     return self ::$instance;
 }
function getdata(){
    echo 'Username is: '.$this->username.' '.' Password is: '.$this->password;
}

function setdata($username,$password){
    $this->username = $username;
    $this->password = $password;
}
 }
?>

Set Data File

<?php 
  include_once 'user.php';
  $user = user::getInstance();
  $user->setdata('MuhammedAtif','1234');
?>

Get Data File

<?php
  include_once 'user.php';
  $user2 = user::getInstance();
  $user2->getdata();
?>

Aucun commentaire:

Enregistrer un commentaire