Originally Posted by
kung foo man
PHP Code:
$_SESSION[user] = $userService->getUser();
A session entry is a serialized piece of data, which cannot hold any resource variables.
Thanks this is the thing I needed to know. I was wrongly thinking that mysqli object was the main problem.
Originally Posted by
kung foo man
read the stuff you need every time a user opens the page, so you don't have any out-of-sync data.
I wanted to make global user class object that would have functions (with mysql queries) returning appropriate data.
Like $user->getSomething(); and it would return information from the database about that something (groups this user belongs etc).
So, now my problem is finding a way to make global object.
Attempt:
index.php
PHP Code:
global $user;
if (isset($user)) {
header("Location: home.php");
}
if (isset($_POST['btn-login'])) {
$userService = new UserService($mysqli, $_POST['identity_number'], $_POST['password']);
if ($userService->login()) {
$user = $userService->getUser();
header("Location: home.php");
}
}
home.php
PHP Code:
global $user;
if (!isset($user))
header("Location: index.php");
After moving to home.php variable $user isn't set..
Reading most comments over this problem directs to Singleton Pattern, but personally I have no clue, how to pass parameters to the instance, so it could contain $id, $name etc.
PHP Code:
class Singleton {
private static $instance;
private function __construct() {}
private function __clone() {}
public static function getInstance() {
if (!Singleton::$instance instanceof self) {
Singleton::$instance = new self();
}
return Singleton::$instance;
}
public function DoSomething() {
...
}
}
Any known sollution to make class object globl score?
Thanks again for your help.
EDIT:
I found sollution:
PHP Code:
<?php
/**
* Created by PhpStorm.
* User: Meh
* Date: 20.11.2016
* Time: 12:51
*/
class DB extends MySQLi {
private static $instance = null ;
private $host = 'localhost';
private $user = "root";
private $password = "Wilki0101";
private $database = "mydb";
public function __construct($host, $user, $password, $database){
parent::__construct($host, $user, $password, $database);
}
public static function getInstance(){
if (self::$instance == null){
self::$instance = new self('localhost', "user", "pass", "db");
}
return self::$instance ;
}
}
Now, when the Class user needs to make queries, it just get's an instance like that:
PHP Code:
include "DB.php";
class User
{
//private static $instance;
public $_id;
public $_name;
public $_lastname;
public $_groupID;
public $_subgroupID;
protected $_db;
public function __construct($id, $name, $lastname, $groupID, $subgroupID)
{
$this->_id = $id;
$this->_name = $name;
$this->_lastname = $lastname;
$this->_groupID = $groupID;
$this->_subgroupID = $subgroupID;
}
public function __toString()
{
return $this->_name . " " . $this->_lastname;
}
public function getUserGroups()
{
$this->_db = DB::getInstance();
$result = $this->_db->query("select p.idpodgrupy, p.nazwapodgrupy from podgrupy p
join grupy_podgrupy g
on p.idpodgrupy=g.idpodgrupy
join grupy gp
on gp.idgrupy = g.idgrupy
join uzytkownicy u
on u.idgrupy = gp.idgrupy
where u.iduzytkownika = 16");
print_r($result);
}
}
And the $results aren't empty.
Thanks anyway kung for your tip with $_SESSION!