Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9083469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:47:00+00:00 2026-06-16T20:47:00+00:00

I know the title doesn’t give much clues about what I’m asking for, so

  • 0

I know the title doesn’t give much clues about what I’m asking for, so here’s the simplified situation:

class MyPDO extends PDO
{
    private $stmt;

    function __construct($dsn...)
    {
        parent:__construct($dsn...);
    }

    function myQuery($sql)
    {
        $this->stmt = $this->query($query);
    }

    function myFetchAll()
    {
        return $this->stmt->fetchAll($mode);
    }

    function myFetchRow()
    {
        return $this->stmt->fetch();
    }

}

Throughout the application I have a base instance of MyPDO and pass it to different objects, mappers.

$adapter = new MyPDO($dsn...);
$adapter->myQuery('SELECT * FROM table');
$rows = $adapter->myFetchAll();

$another_object = new ObjectThatNeedsPDO($adapter);
$another_object->adapter->myQuery('SELECT * from another_table');
$rows = $another_object->adapter->myFetchAll();

Is this approach safe, especially from the MyPDO::stmt perspective? Can the application flow mess things up so I can end up fetching data from another $stmt than expected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:47:01+00:00Added an answer on June 16, 2026 at 8:47 pm

    Personally, I would not take your approach. The reason being that I would not want the possibility of a statement object created by one class being exposed to another unrelated class. Additionally, each implementing class may have different sorts of parameter binding which it needs to perform, ways in which it needs to access the data (i.e. fetch all rows, fetch each row, fetch as objects vs. arrays, etc.), ways in which to handle errors in a class-specific way, and so forth.

    To me you are gaining nothing by having this logic inside of some child of the base PDO class. I mean is it really much harder to do:

    $stmt = $this->pdo->query(...)
    $data = $stmt->fetchAll();
    

    than:

    $this->myPDO->query(...);
    $data = $this->myPDO->myFetchAll();
    

    What do you gain other than a unnecessary coupling of this additional class to all the classes which will consume it? Really, the statement interactions are always going to be very class specific with really the only common functionality (the DB connection) being provided by the base PDO instance.

    As such, certainly feel free to pass around a common PDO instance amongst classes, this is definitely a good practice (i.e. dependency injection).

    Just think really hard about whether you want to potentially change every single implementing class when you make changes to your proposed myPDO class, or whether you want to change your myPDO class every time some implementing class needs some custom means to interact with the statement object.

    Based on discussions below, it seems you may want to consider extending PDOStatement to give you the maximum flexibility.

    This might look like this:

    class myPDOFactory {
        public static function getInstance($dsn, $pdo_statement_class = 'myPDOStatement', $pdo_constructor_args = NULL);
            $pdo = new PDO($dsn);
            if (empty($pdo_statement_class)) {
                $pdo_statement_class = 'PDOStatement';
            }
            if (empty($pdo_constructor_args) || !is_array($pdo_constructor_args)) {
                $pdo_constructor_args = array();
            }
            $config_array = array($pdo_statement_class, $pdo_constructor_args);
            $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, $config_array);
            return $pdo;
        }
    }
    
    class myPDOStatement extends PDO Statement {
        public function __construct(<any custom parameters you may need to have passed - items in $pdo_constructor_args from myPDOFactory class>) {
            parent::__construct();
            // any special stuff you want to do with any passed parameters here
        }
    
        public function fetchAll() {
            // override any functionality you desire here
        }
    
        public function fetchAllObjects() {
            return $this->fetchAll(PDO::FETCH_OBJ);
        }
    }
    
    class someClassThatNeedsPDO {
        protected $pdo = NULL;
        public function __construct($pdo) {
            if($pdo instanceof PDO) {
                $this->pdo = $pdo;
            } else {
                throw new Exception('Ooops!');
            }
        }
    
        public function doSomethingWithPDO() {
            $stmt = $this->PDO->prepare('SELECT * FROM sometable');
            $stmt = execute();
            return $stmt->fetchAllObjects();
        }
    }
    

    Usage example:

    $pdo = myPDOFactory::getInstance($dsn, 'myPDOStatement', $constructor_args);
    $consuming_class = new someClassThatNeedsPDO($pdo);
    $object_array = $consuming_class->doSomethingWithPDO();  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that title sounds crazy but here is my situation. After a certain
I know the title doesn't capture what I'm trying to ask but here is
Sorry, that title probably doesn't make much sense, but what I want to know
I know the question title doesn't make much sense, but I can't think of
I know that the title doesn't really say what I'm actually looking for, as
I know the title of this question is a bit confusing, but here it
Ok I know the title doesn't fully explain this question. So I'm writing a
Ok I know the title doesn't really tell you what my problem is but
The title is horrible, i know; I'm terrible at titles on SO here. I'm
Title might be confusing, didn't quite know how to put it. Here's what i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.