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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:11:51+00:00 2026-06-17T12:11:51+00:00

I often use properties in my classes that store an array of options. I’d

  • 0

I often use properties in my classes that store an array of options. I’d like to be able to somehow merge those options from defaults declared in a parent class.

I demonstrated with some code.

class A
{
    public $options = array('display'=>false,'name'=>'John');
}

class B extends A
{
    public $options = array('name'=>'Mathew');
}

Now when I create B, then I’d like $options to contain a merged array from A::options

What happens now is this.

$b = new B();
print_r($b);
array('name'=>'Mathew');

I would like something like this using array_merge_recursive().

array('display'=>false,'name'=>'Mathew');
  • Maybe it’s something I could do in the constructor?
  • Is it possible to make this a behavior of class A? So that I don’t always have to implement the same code in all subclasses.
  • Could I use reflection to auto find array properties in both classes and merge them?
  • 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-17T12:11:52+00:00Added an answer on June 17, 2026 at 12:11 pm

    I realize I changed your interface from a public variable to a method, but maybe it works for you. Beware, adding a naive setOps($ops) method may work unexpected if you allow the parent ops to continue to be merged in.

    class A
    {
        private $ops = array('display'=>false, 'name'=>'John');
        public function getops() { return $this->ops; }
    }
    class B extends A
    {
        private $ops = array('name'=>'Mathew');
        public function getops() { return array_merge(parent::getOps(), $this->ops); }
    }
    class c extends B
    {
        private $ops = array('c'=>'c');
        public function getops() { return array_merge(parent::getOps(), $this->ops); }
    }
    
    $c = new C();
    print_r($c->getops());
    

    out:

    Array
    (
        [display] => 
        [name] => Mathew
        [c] => c
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often use convenience functions that return pointers to static buffers like this: char*
I'll often have objects with properties that use the following pattern: private decimal? _blah;
Often, when I need to store system properties like admin info, versions, and so
I often use Ctrl + P to open files, but then I'd like to
I quite often use Drupal's Views Module to build SQL that I paste into
I have a (dump) question regarding VB/C# I often use third party classes where
Since I often use TemplateSelectors that distinguish between templates based on Type, I tried
I make heavy use properties from bundles in my application as I strive to
A typical CSS property that I use often is overflow-x or overflow-y . Sometimes
I have SAP RPC OCX control that I'd like to use. In C# 4

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.