user1547410
user1547410

Reputation: 893

Managing several classes in php

Trying to get my head around classes and OOP

I have the following situation and i define my classes as such

// Include database connection class
require_once INCLUDE_PATH.'database.class.php';
$database = new Database;

// include the functions helper class
require_once INCLUDE_PATH.'functions.php';
$functions = new functions();

// include the error handling class
require_once INCLUDE_PATH.'error_handling.php';
$errorHandling = new errorHandling();

database.php

class Database{

    public function __construct(){

    // Create dsn connection string
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;

    $options = array(
        // avoids duplicate connections being opened
    PDO::ATTR_PERSISTENT    =>  true,
    // sets error mode if a database error is thrown
    PDO::ATTR_ERRMODE       =>  PDO::ERRMODE_EXCEPTION
    );  

    // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        } catch(PDOException $e){ // Catch any errors
            $this->error = $e->getMessage();

        // email the administrator and redirect and display a user friendly message for the user
        $errorHandling->database_connection();
        }

    } // end of construct function to setup database connection
} // end of database class

functions.php

class functions{

    public function email_admin {

    // run code to email the administrator with the error
    }
}

error_handling.php

class errorHandling{

public function database_connection(){

    // this handles the code that holds the errors for
    // both the admin message and user message and redirects
    // the user to a page to show the user friendly error
    // if the database connection fails
    // it also runs the code to email the admin before redirecting

    $functions->email_admin();

}

So you can see what im trying to do and most likely quite badly. I have extensive error handling for user and admin so im using a class for that so i don't clutter up the code. Im using a helper function and im using the database class which has all the sql stuff.

There are several situations where a database function is using functions in both error handling and functions for emailing the admin. I could just lump everything into 1 big class and solve my issue but that wouldn't be efficient.

I can use global $functions and global $errorHandling within each individual function when i need it but that seems repetitive. Is there a better way to handle this or can you point me to an article or reading that deals with this. Ive tried googling but a lot of the tutorials are very general and im finding it difficult to find something that deals with my specific issue. I also know about extend but it doesn't feel like an appropriate situation for extending the database class and i can still only extend one class i believe.

The problem comes when i try and use:

$errorHandling->database_connection();

within the database class. If i do that i get an error: Call to a member function database_connection() on a non-object or something similar

Upvotes: 0

Views: 208

Answers (1)

jeroen
jeroen

Reputation: 91734

Your current problem is a variable scope problem: $errorHandling is not defined in the scope of the method in the Database class, so you are trying to call the database_connection() method on an undefined thing (for lack of a better word...).

If your Database class depends on several other classes and inheritance is not the obvious way to as there is no real inheritance relationship, you might want to look into Dependency Injection. In short, when you construct a Database object, you inject the necesary dependancies in the object and assign them to properties of that object so that you have them available there.

A simple example:

Main code:

$errorHandling = new errorHandling();
$database = new Database($errorHandling);

In the Database class:

class Database{

    private $_errorHandler;

    public function __construct(errorHandling $errorHandling){

        $this->_errorHandler = $errorHandling;

        // the rest of your code

            // ...
            $this->_errorHandler->database_connection();

By the way, you should also check out Autoloading of classes, that will make your life a lot easier as the number of classes grows.

Upvotes: 1

Related Questions