Jump to content

Class Help


 Share

Recommended Posts

This is the first time I wrote a class, so I want to make sure this is right. Under "class SafePDO_errordisplay extends SafePDO" am I using "parent::__construct($dsn, $username, $password, $driver_options);" correctly? It should be sending the connection data to SafePDO to start the connection. The errordisplay class is there to catch any error on the connection.

 

class SafePDO extends PDO {

        public static function exception_handler($exception) {
            // Output the exception details
            die('Uncaught exception: '. $exception->getMessage());
        }

        public function __construct($dsn, $username='', $password='', $driver_options=array()) {

            // Temporarily change the PHP exception handler while we . . .
            set_exception_handler(array(__CLASS__, 'exception_handler'));

            // . . . create a PDO object
            parent::__construct($dsn, $username, $password, $driver_options);

            // Change the exception handler back to whatever it was before
            restore_exception_handler();
        }

}

class SafePDO_errordisplay extends SafePDO {

public function connect_db($dsn, $username='', $password='', $driver_options=array()){

	parent::__construct($dsn, $username, $password, $driver_options);
		try {
		$DB = new SafePDO($dsn, $user, $password, $driver_options);
		}
		catch (PDOException $e) {
		echo 'Connection failed: ' . $e->getMessage();
		}
}
}
// Connect to the database - So my other coders only need to know the dbname to connect, not the MySQL log-in details. 
function SafePDOPersist($dbname){
$DB = new SafePDO_errordisplay("mysql:host=localhost;dbname=$dbname", $DBusername, $DBpassword, array(PDO::ATTR_PERSISTENT => true));
}

function SafePDOCOE($dbname){
$DB = new SafePDO_errordisplay("mysql:host=localhost;dbname=$dbname", $DBusername, $DBpassword);
}

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.