0

here is my problem. I am trying to create one big file (full of functions) which will call the object functions. I want to create some SUPERGLOBAL variable which will contains everything about that called class (even between functions) class like "user". What data? Just something like user's ID, Name, Email, Level from database so I don't need to using SQL SELECT from USERS in almost every function in this file.

This is how my core.php file looks like (it's cut just to explain what am I trying to do)

//File -- core.php
require_once "pdo.php"; //Includes the $db -- pdo connection to database
require_once "user.php"; //Including the object
$GLOBAL["user"] = new USER($db);

function login($email, $pswd){
  $GLOBAL["user"]->login($email, $pswd); //LOGIN and also in this class function are written the data like $GLOBAL["user"]->ID, etc
  //do something more
}

function ShowLoggedUser(){
   echo $GLOBAL["user"]->ID; //But now here it's not working -> it's empty, nothing there
   echo $GLOBAL["user"]->Email;
   //etc

}

And the thing is, that's I in the object USER I am setting the public data like

//file -- user.php
class USER
{
    //Public data

    public $Name;
    public $Level;
    public $ID;
    public $Email;

    private $DB;

    function __construct($DB_con)
    {

      $this->DB = $DB_con;      
    }
    public function login ($email, $password)
    {
      //Some SQL and verifying user with DB
      //if everything is ok do this:
      foreach($result as $data){ //Results from SQL -- details about user
        $this->ID = $data["ID"];
        $this->Name = $data["Name"];
        $this->Email = $data["Email"];
      }
      return "some message";
    }

The function Login is the key of this file, if the login won't be used, then the user is kicked out of this file (it's file just for logged users), so there is 100% guaranty that's the data in class are written and they really shows up when I echo them in the function "login". But anywhere else it's just totally empty...

Thank you for any help / tips!

Jan Kocvik
  • 41
  • 8
  • 1
    You are doing it wrong. Instead of making a dirty hack via superglobals, you should use dependency injection instead. Also, FYI, exposing the object's internal variables as `public` breaks the encapsulation ... thus making the code even harder to maintain. Oh, and read about how to use autoloaders. – tereško Nov 22 '17 at 22:56
  • I didn't think it will work this way but I'll try thank you! – Jan Kocvik Nov 22 '17 at 22:58
  • It's `$GLOBALS` not `$GLOBAL`; but it isn't good using globals to pass variables around anyway – Mark Baker Nov 22 '17 at 23:04
  • Not working anyway, but thank you I totally missed that detail! :) – Jan Kocvik Nov 22 '17 at 23:06
  • "Not working" is not a known php error code. Maybe since it is not working, it should get its ass of the couch and go find a job or something. – tereško Nov 22 '17 at 23:08
  • Trying to solve this whole week, it's just a detail but I am trying to create more effective code so I can use this knowledges in project to final exam. The code is useless when it's get slow just because programmer using sql in every function. Yes I can use sessions, but I think it's really bad solution yet still working solution. So I am just trying to get knowledges about this, because I am already running out of ideas that's why I asked here. – Jan Kocvik Nov 22 '17 at 23:12
  • 1
    How much time do you have? Because I think you have a lot to learn. First of all, to have a user-login functionality, you need to store that state between requests. And all the variables get destroyed between requests. Your only option is to use either session or cookies. – tereško Nov 22 '17 at 23:20
  • About 5 months? Actually the project is about 70% complete but with this kind of "bad" code so I want to rewrite it. I'll learn about DI and how to work with autoloaders, thank you for your comment! :) – Jan Kocvik Nov 23 '17 at 18:17
  • To implement login system, you actually need to start with learning how sessions work. You also should read this: https://stackoverflow.com/questions/549/the-definitive-guide-to-form-based-website-authentication ... then move on to autoloaders and read [this](https://stackoverflow.com/a/19309893/727208). – tereško Nov 23 '17 at 19:37
  • Ok amazing I am on it thanks! – Jan Kocvik Nov 24 '17 at 19:00

0 Answers0