0

I have 3 files:

main.php (all the site code) AccountHandler.class.php (handles login/logout/get acc details) User.class.php (Plain object, getters and setters for generic user traits)

In the 'login' function in AccountHandler, it checks the password is correct and then returns the details on file for the account. I wish to then instantiate a 'User' object and fill it with these details, returning said object from the function.

However, on the other end, in main.php where the function was originally called i need to be able to recognise the 'User' object and access the values.

How can i include the User class file in BOTH the AccountHandler class AND the main.php such that they can both use the 'User' class?

Does any of that make sense?

class User
{

private $email;
private $username;
private $role;

public function __construct($email, $username, $role)
{
    $this->setUsername($username);
    $this->setEmail($email);
    $this->setRole($role);
}

public function getEmail(){
    return $this->email;
}

public function setEmail($email){
    $this->email = $email;
}

public function getUsername(){
    return $this->username;
}

public function setUsername($username){
    $this->username = $username;
}

public function getRole(){
    return $this->role;
}

public function setRole($role){
    $this->role = $role;
}


}
Broak
  • 3,978
  • 4
  • 27
  • 54
  • Not sure I understood this clearly, but I would use `require_once("user_class.php")` inside the topmost(Assuming you are adding this in `main.php` which also imports `account-handler`) file. That should wok – Kamehameha Jul 15 '15 at 16:35
  • You instantiate the User class in a function of AccountHandler class and returning the User object. And you call the AccountHandler function from main.php. If this is the case, just assign the returned object to a variable. – frz3993 Jul 15 '15 at 16:41

2 Answers2

0

By creating a resources object you may then pass objects around by reference.

Here is some sample code.

Main.php

$resources = new stdClass;
$account = new account($resources);
$account->checkAccount();
if(isset($resources->user)){
   var_dump($resources->user);
}

Account class

class account{

  private $resources;
  private $loginSuccess = false;

  public function __construct($resources){
     $this->resources = $resources;
  }

  public function checkAccount(){

    if($this->loginSuccess){
       $this->resources->user = new user($email, $username, $role);
    }   

  }
}

You may also find this useful

Community
  • 1
  • 1
andrew
  • 8,614
  • 7
  • 25
  • 56
0

Turns out i can use autoload:

spl_autoload_register('autoloader');

function autoloader($classname) {
    include_once 'php/includes/' . $classname . '.class.php';
}

at the top of my main.php, instantiate the AccountHandler and use the User class within it and have everything included automatically! Thanks all.

Broak
  • 3,978
  • 4
  • 27
  • 54
  • I thought you said you wanted to access the user class from within main.php and the account class? I do not see how this answer accomplishes that – andrew Jul 15 '15 at 17:13
  • Yes, this seems to accomplish that, i can create user objects in both files now – Broak Jul 15 '15 at 17:14
  • They will be separate instances of the user class, and you will not be able to share properties between them. It seems a bad approach. – andrew Jul 15 '15 at 17:15