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 6850305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:29+00:00 2026-05-27T01:06:29+00:00

I am trying to write a PDO wrapper but I’m having some problems with

  • 0

I am trying to write a PDO wrapper but I’m having some problems with the constructor. Ideally, I would like to call the parent’s constructor but, for some reason, that is not working. I tried (to test) to check if creating a new PDO and that does work, which I find most confusing.

Here is my code:

class db extends PDO {

    private $dbconn;

    public function __construct() {
        $dsn = 'mysql:dbname=' . MYSQL_DB . ';host=' . MYSQL_HOST;
        $user = MYSQL_USER;
        $pw = MYSQL_PW;
        try {
            $this->dbconn = parent::__construct($dsn, $user, $pw);
            $this->dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            return $this->dbconn;
        }
        catch(PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();;
        }


    }
}

It works if I replace the parent:: line with $this->dbconn = new PDO($dsn, $user, $pw);

I believe that the “correct/elegant” way to do it is to use the parent:: syntax so I would like to know why that is not working/how I can fix it. Anyone can help?

Thank you!

  • 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-05-27T01:06:29+00:00Added an answer on May 27, 2026 at 1:06 am

    That’s not how you use constructors, as they don’t return anything. Try the following instead:

    public function __construct() {
        $dsn = 'mysql:dbname=' . MYSQL_DB . ';host=' . MYSQL_HOST;
        $user = MYSQL_USER;
        $pw = MYSQL_PW;
        try {
            parent::__construct($dsn, $user, $pw);
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();;
        }
    }
    

    NOTE: You may still run into issues, as your constructor doesn’t accept the same arguments as the PDO constructor. One of the tenets of OOP is that of equivalence, as embodied in the Liskov Substitution Principle, which means that the protocol (public API) of a subclass should be a strict superset of its superclass. The reason this is important is that if the subclass has an API that differs from the one presented by the class it inherits from, then it can’t be used to substitute for the superclass in all cases.

    For example, how would you use your subclass to connect to a PostgreSQL database or use a SQLite file instead of using mysql? The PDO superclass can work with all three, along with other database back ends, by virtue of the fact you can pass a DSN in as an argument, but you can’t do that with your subclass.

    However, all this is getting into aspects of computer science and is drifting off topic somewhat 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an iterator for results from a PDO statement but I
Trying to write a simple call to google's JSON stock ticker API. I would
I'm trying to work with PDO class on php but I have some trouble
Im trying to write a footer like this one Did i said that im
I'm trying write a simple perl script that reads some fields from a password
Trying to write a trivial application, But I have stuck into one of the
I've been trying to use SQLite with the PDO wrapper in PHP with mixed
All, I'm trying to write a function that would check in my users table
Am trying to write a query that would output something similar to the last
I'm trying to write a simple, full text search with PHP and PDO. I'm

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.