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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:54:17+00:00 2026-05-29T05:54:17+00:00

Disclaimer: This is not homework. Also, I’m describing a simplified situation so bear with

  • 0

Disclaimer: This is not homework. Also, I’m describing a simplified situation so bear with my semi-psudo code.

I have what I feel is a pretty standard OOP situation, but I’m not sure the best way to handle it. Let’s say I have 3 classes: Base, A, and B. Class A extends Base and B extends A. The Base class’s constructor performs a database query that pulls various meta data defined in an array. See below:

Class Base {

    public $meta_data = array('col1','col2');

    function __construct() {
        $db->query("select ".join(',',$this->meta_data)." from table");
    }

}

Class A extends Base {

    public $meta_data = array('col3','col4');

    function _construct() {
        parent::__construct();
    }

}

Class B extends A {

    public $meta_data = array('col5','col6');

    function _construct() {
        parent::__construct();
    }

}

When I create a new B() object, I want the Base constructor’s sql query to look like this:

select col1, col2, col3, col4, col5, col6 from table;

And when I create a new Base() object, I want the sql query to look like this:

select col1, col2 from table;

Now…I technically know how to accomplish this (in possibly a crude way) but I’m curious what the proper way is without too much code replication and keeping in mind flexibility for the future (ie, adding a subclass of B). I basically need to merge the $meta_data arrays on each inheritance level all the way up to the constructor in the Base class.

Any ideas?

  • 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-29T05:54:18+00:00Added an answer on May 29, 2026 at 5:54 am

    When you extend the class and define the new property you are overriding the values set in the parent class. I would wind up removing the $meta_data array from all but Base. Then in your extending classes create a function similar to:

    After solving it in a very simple way I wound up deciding I would go with something like the following in Base:

    protected function addToMetaData(array $meta_data) {
        $this->meta_data = array_merge($this->meta_data, $meta_data);
    }
    

    Then in your extending class constructors you just pass in an array of the values you want to add to Base::$meta_data.

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

Sidebar

Related Questions

Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer: This is not actually a programming question, but I feel the audience on
Full disclaimer: this is not really a homework, but I tagged it as such
( Disclaimer: This question is not specific to ASP.NET) I have a control which
Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer: this is not a question about how to install asp.net or an application
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less
Background / Disclaimer First of all, please feel free to skip this entirely if
DISCLAIMER: This is not a real-world example. It is just a theoretical question of
Disclaimer : I have seen the exact questions, but have not seen any answers

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.