I've recently picked up OOP in Java and I'm trying to use it for my PHP usersystem.
I've come up with the following generic.class. On my main site, I'll simply put a include('generic.class.php');
before my content.
<?php
include_once('connect.class.php');
class User {
private $id;
private $username;
private $email;
public function __construct(){
$this->id = $_SESSION['id'];
$this->username = $_SESSION['username'];
$this->email = $_SESSION['email'];
}
}
class Generic {
private $maintenance = false;
public function __construct() {
// Start session
session_start();
// Check maintenance status
if($this->maintenance){
include('maintenance.php');
die();
}
if(user_logged_in()){
$user = new User();
}
}
}
function user_logged_in(){
if (isset($_SESSION['username'], $_SESSION['id'])){
return true;
} else {
return false;
}
}
$generic = new Generic();
$conn = new Conn();
?>
Echoing $user->username
doesn't seem to work. Or is there no need to use a User
class at all? (most PHP usersystems I see don't have a User
class)
Is this the right approach to using OOP and classes with a usersystem? If not, how should my classes be like?