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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:25:45+00:00 2026-06-14T10:25:45+00:00

Given the following class structure: class Foo { protected static $_things = [‘thing’]; }

  • 0

Given the following class structure:

class Foo {
    protected static $_things = ['thing'];
}

class Bar extends Foo {
    protected static $_things = [
        'thing', 'other-thing'
    ];
}

class Baz extends Bar {
    protected static $_things = [
        'thing', 'other-thing', 'something-else'
    ];
}

class Quux extends Baz {
    // Note the lack of "other-thing"
    protected static $_things = [
        'thing', 'something-else', 'one-more'
    ];
}

What would be the best way to refactor this and keep the array elements more DRY? For example, the “thing” element should only be defined once (in Foo), the “other-thing” element should only be define once (in Bar), and so on.

In practice, this array is very large, and there can sometimes be up to 4 or 5 levels of inheritance, each needing to “modify” this array in some special way, whether that’s adding or removing elements.

I’ve been toying with the idea of initialization methods that would do the appropriate modifications, but wanted to see if there were any better ways first.

  • 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-14T10:25:46+00:00Added an answer on June 14, 2026 at 10:25 am

    Simplest solution I can think of (heavily based on the singleton pattern).

    I don’t think there is any compile-time way to do what you are looking for.

    <?php
    class A {
        private static $a = [1, 2, 3];
        public static function getA() {
            return self::$a;
        }
    }
    
    class B extends A {
        private static $a = null;
        public static function getA() {
            if (self::$a === null)
                self::$a = array_merge(A::getA(), [4, 5, 6]);
            return self::$a;
        }
    }
    
    echo join(',', B::getA()) . "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following class structure, will Bar serialize/deserialize as expected? public class Foo {
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following structure... class Foo { public string Category { get; set; }
Given the following HTML structure: <input class=addon-thumb type=checkbox name=foo/><label for=foo><img class=addon-thumb src= title=bar alt=foo/></label>
Given the following in PHP: <?php class foo { public $bar; function __construct() {
I want to override the tree_id field as following: Given: class Thing(MPTTModel): thing_id =
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following class structure: class Base { virtual void outputMessage() { cout <<
Given the following structure: Public Class Vendor Public Property Accounts As Account() End Class
Given a node with the following structure class Node { int data, Node* P1,

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.