0

I'm not trying to create my own PDO class, just extend it so I can insert a try catch statement in my own execute function (myexecute) so I do not have to write the code every time.

Firstly, is this a good ideas? or should I just scrap it?

so I want this:

$DB = new Database ( HOST, DB, USER, PASS );
$query = $DB->prepare("INSERT * FROM books WHERE title = :title");
$query->bindParam( ':title', $_POST['title'] );
$query->myexecute();

if there was a problem my function (myexecute) would sort the error handling the problem is Im getting this error:

Fatal error: Call to undefined method PDOStatement::myexecute()

Any ideas what Im doing wrong?

 class Database extends PDO
    {
    private $dbh;
    private $error;
    private $total;
    private $p_query;


    public function __construct($hostname,$dbname,$username,$password)
    {
        try  
        { 
            $dsn = 'mysql:host='.$hostname.';dbname='.$dbname;
            parent::__construct($dsn, $username, $password);
        } 
        catch(PDOException $e)
        {
            echo "DataBase Error: Connection error.<br>".$e->getMessage();
            exit;
        }
    } 

    public function myexecute()
    {
        try 
        {
            return parent::execute();
        } 
        catch (PDOException $e) 
        {
            echo "DataBase Error: The user could not be added.<br>".$e->getMessage();
            exit;
        }   
        catch (Exception $e) 
        {
            echo "General Error: The user could not be added.<br>".$e->getMessage();
            exit;
        }
    }
}

3 Answers3

0

To do what you are trying to do, you'd have to write a wrapper around PDOStatement and override the prepare method in your Database class to return that wrapper of PDOStatement. So that you can have a method myexecute in your subclass of PDOStatement.

Sylvain
  • 542
  • 6
  • 20
0

Wanting to extend PDOStatement is quite common and is supported by PDO. You just need to specify which class to use for statements.

class DBIStatement extends \PDOStatement
{
    public function fetchrow_hash()
    {
        $rows = $this->fetchAll();
        return (count($rows) == 1) ? new DBIDataRow($rows[0]) : new DBIDataRow(array());
    }
}
class DBI
{
    public $dbh;

    public function __construct($type, $host, $name, $user, $pass)
    {   
        $dsn = sprintf('%s:host=%s;dbname=%s',$type,$host,$name);
        $this->dbh = new \PDO($dsn,$user,$pass,array(
            \PDO::ATTR_ERRMODE,           PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC,
        ));
        // *** This is what tells PDO to use your statement class.
        $this->dbh->setAttribute(\PDO::ATTR_STATEMENT_CLASS,array('DBIStatement'));
    }

Don't be alarmed by the back slashes. This is part of a namespaced application.

The only strange thing is that I could not pass the statement class in the pdp constructor like I could wit the other attributes. But is does work.

No need to fool with the pdo prepare method at all.

Cerad
  • 48,157
  • 8
  • 90
  • 92
0

You don't have to write try catch statement with raw PDO either.

So, the reason you are trying to extend PDO for is a false one.

Just quit that bad habit of writing try catch on every statement and you'll be fine with raw PDO.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345