#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    391
    Rep Power
    19

    Extend PDO and extend PDOStatement


    I am trying to extend PHP's PDO and PDOStatement classes.

    The major hurdle I've come up against is getting my PDO class to use my PDOStatement class.

    PHP Code:
     
    // myPDO.php
    class myPDO extend PDO
    {
        public function __construct( $dsn, $username=NULL, $password=NULL, $options=array() )
        {
             parent::__construct($dsn, $username, $password, $options)
             $this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array('myPDOStatement',array($this)) );
        }
     
        public function query ( $statement, $a1=NULL, $a2=NULL, $a3=NULL )
        {
            if ( !is_null($a1) && !is_array($a1) ) :
                $stmt = $this->prepare( $statement );
                $stmt->setFetchMode( $a1, $a2, $a3 );
                $stmt->execute();
            else :
                $stmt = $this->prepare( $statemtent );
                $stmt->setFetchMode( PDO::FETCH_ASSOC );
                $stmt->execute( $a1 );
           endif;
     
           return $stmt;
        }
     
    }
     
    // myPDOStatement.php
    class myPDOStatement extends PDOStatement
    {
     
    }


    Here are the warnings and errors I'm getting

    Code:
    PHP Warning: PDO::prepare() [<a href='pdo.prepare'>pdo.prepare</a>]: SQLSTATE[HY000]: General error: user-supplied statement does not accept constructor arguments in /myPDO.php on line 119
    PHP Warning: PDO::prepare() [<a href='pdo.prepare'>pdo.prepare</a>]: SQLSTATE[HY000]: General error: failed to instantiate user-supplied statement class in /myPDO.php on line 119
    PHP Warning: PDO::prepare() [<a href='pdo.prepare'>pdo.prepare</a>]: SQLSTATE[HY000]: General error in /myPDO.php on line 119
    PHP Fatal error: Call to a member function setFetchMode() on a non-object in /myPDO.php on line 121
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,960
    Rep Power
    9397
    A quick glance through the PHP source code suggests that your class needs a constructor. A private or protected one, at that.
    To check if PDO tries to pass arguments,
    PHP Code:
    class myPDOStatement extends PDOStatement {

        private function 
    __construct() {
            echo 
    "myPDOStatement constructor arguments: ";
            
    var_dump(func_get_args());
        }


    Let me know the results: I might file this as a bug (if there isn't one already).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    391
    Rep Power
    19
    There were 2 issues with my code.

    1. I miss-typed "$statement" on line 18
    2. Not mentioned, I was attempting to use a persistent connection

    I think I might just make a couple of wrapper classes, because I think the persistent connection is useful.

    Any one have any perspective on the persistent connection?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Try this


    Code:
    class epdo extends PDO {
    	public function __construct($dbname, $server='127.0.0.1', $username='marmalade', $password='nkm350fr') {
    		  parent::__construct("mysql:host=$server;dbname=$dbname", $username, $password);
    		  parent::setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    	}
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0
    I think I might just make a couple of wrapper classes

IMN logo majestic logo threadwatch logo seochat tools logo