2

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
Jrn
  • 1,185
  • 1
  • 13
  • 27
  • Use an autoloader to avoid a myriad of includes (or use include_once instead, and inject your database connection into any classes that need it – Mark Baker May 20 '13 at 15:05
  • 1
    Read up on autoloading http://php.net/manual/en/language.oop5.autoload.php – fullybaked May 20 '13 at 15:05
  • You also could use a singleton class that makes sure only one instance of the class runs, though autoloading is probably better – edwardmp May 20 '13 at 15:06
  • you might find [this](http://stackoverflow.com/a/11369679/727208) relevant – tereško May 20 '13 at 15:50
  • Thank you Mark for suggesting autoloader. I've looked it up and it looks very usefull. Thanks – Jrn May 20 '13 at 16:43

3 Answers3

2

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);
}
cipher
  • 2,414
  • 4
  • 30
  • 54
1

In most PHP coding standards will say the only thing a "class file" should contain is the class definition. That means no includes or requires. 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.

VettelS
  • 1,204
  • 1
  • 9
  • 17
  • Thanks for the info, I've looked up on autoloader in PHP and I'm using it right now, Thanks. – Jrn May 20 '13 at 16:43
1

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.

Orangepill
  • 24,500
  • 3
  • 42
  • 63