Jump to content

Class Help


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
https://forums.phpfreaks.com/topic/263414-class-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.