Reputation: 1235
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
Upvotes: 2
Views: 3541
Reputation: 1224
In most PHP coding standards will say the only thing a "class file" should contain is the class definition. That means no include
s or require
s. So how do you allow one class to access another?
Autoloading! Go and read about autoloading, but the brief explanation is that you define an autoloader in a separate file (the same file that you actually use the classes in), and any classes that you call, and any classes that are required by those classes, PHP goes and fetches automatically. Autoloading is great, not only because you don't have to bother typing include
statements, but also because only the classes you actually use are fetched- so it's more efficient.
Another point to make, it's usually a good idea you keep all of your classes in their own folder, and then actually use those classes from outside that folder. Again, it's just a convention, but it also makes implementing autoloading a little easier.
Upvotes: 1
Reputation: 24645
What you want to look into is the PSR-0 Standard https://gist.github.com/Thinkscape/1234504
Most off the shelf autoloaders work easiest with this standard.
Upvotes: 1
Reputation: 2484
You will not be connecting to database three times in a row, if you follow just these simple steps.
Replace
include 'database.php'
to
include_once 'database.php'
and writing your connection code somewhat like this
if(!$con) {
$con = mysqli_connect(HOST, USER, PASS);
}
Upvotes: 2