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

  • Home
  • SEARCH
  • 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 6815875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:51:32+00:00 2026-05-26T20:51:32+00:00

Can I extend the constructor of the sueprtype? documentation doesnt say anything about extending

  • 0

Can I extend the constructor of the sueprtype? documentation doesnt say anything about extending functions.

I want to create a class which connects to a database when the subtypes are initialized but I don’t want to write a connection code for each subtype. So I’d like to do it in the constructor of the supertype but I also want subtypes to have constructors.

  • 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-26T20:51:33+00:00Added an answer on May 26, 2026 at 8:51 pm

    Yes, you can override the constructor just like any other method in a class:

    <?php
    class DbSuper {
        function __construct() {
            echo "db connection using super class<br/>";
        }
    }
    
    class DbSub extends DbSuper {
        function __construct() {
            echo "db connection using sub class<br/>";
        }
    }
    
    class DbSubNoConstr extends DbSuper {
    
    }
    
    class DbSubBothConstr extends DbSuper {
        function __construct() {
            echo "execute subclass code<br/>";
            parent::__construct();
        }
    
    }
    
    new DbSub(); //prints db connection using sub class
    new DbSubNoConstr(); // prints db connection using super class
    new DbSubBothConstr(); // prints execute subclass code db connection using super class
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want extend class which have final constructor (in my case it's SimpleXMLElement), but
My goal is to create a trait that a case class can extend, which
In extjs you can always extend an extjs class via the constructor() . For
In Ruby, there's Modules and you can extend a class by mixing-in the module.
I want button in vertically, for that i can extend the height and shrink
How can I extend a builtin class in python? I would like to add
I want to inherit to extend java.util.logging.Logger . The only visible constructor is protected
I noticed that I can extend a class and at the same time pass
if my class is private and constructor is public then what will happen. can
I have half a dozen classes which all extend the same abstract class. The

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.