I'm fairly new too OOP and PHP and I've been reading up on tutorials and books to get the hang of it. Right now i'm working on an personal project (no tutorial or book exercise) and I've got a question on how to organise my classes and files.
I've got a database.php file in which I create my connection with my database, and in the __construct function of my classes i call upon it, like so:
<?php
include 'database.php';
class users {
// Database Connection
public function __construct() {
$db = new database();
}
// the rest of my functions
?>
The problem/question I have is that in the tutorials and exercises i did, there was only one class. Currently in my project I have 3 classes (users, movies and watchlists) and they all have a .php file (users.php, movies.php and watchlists.php)
On certain pages in my project I need data from all 3 classes so I start those pages like so:
<?php
session_start();
include 'include/users.php';
include 'include/movies.php';
include 'include/watchlists.php';
$user = new users();
$movie = new movies();
$watchlist = new watchlists();
// the rest of the file
Now, its pretty obvious to me that this is not the right way when you have multiple classes, as I'm creating a database connection three times in a row.
So how do I manage my classes, database connection and .php files correctly when working on a project like mine?
You don't have to explain the whole thing, if you can link me to some great articles/tutorials about this, it will be greatly appreciated. (Yes, I have googled it but couldn't find anything helpful).
Thanks