I’m relatively new to the PHP OO concept. I’ve been messing around with the following,
<?php
require('class_database.php');
require('class_session.php');
class something {
$database = NULL;
$session = NULL;
function __construct() {
$this->database = new database();
$this->session = new session();
}
function login() {
$this->session->doLogin();
}
}
?>
in another script
$something = new something();
$something->login();
In this example, $database has a constructor which creates a protected variable containing a MySQLi connection. It also has a function called query().
If $session needed to run $database->query, how can I go about it? Creating a new database class would be highly wasteful but I can’t seem to access $database from $session.
Have I created a big hassle for myself I should have avoided? I’m wanting session to provide login verification which would need to access the database to check credentials.
Thanks for any help
How about making the constructor of the session class take a parameter that is the instance of database?
Then in the session constructor, set it to a property of the object and make use of it: