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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:16:47+00:00 2026-05-12T05:16:47+00:00

I have a configurable report. For each field that can be included on the

  • 0

I have a configurable report. For each field that can be included on the report, there’s a key (stored in report preferences), a label, potentially an access level, and a SQL descriptor — something like foo as my_foo.

In a Java app, I would create a class called ReportField with each of the properties listed above. I’d use a private constructor, and list each of the fields in the class like this:

public final static ReportField FOO = new ReportField('foo', 'Foo', 1, 'foo as my_foo');

I’d probably create a static array of all of the fields, add a static method that allows a field to be looked up by key, and so forth. Then in other places I could write code like:

List<String> selectFields = new ArrayList<String>();
for (ReportPref pref : reportPrefs) {
    selectFields.add(ReportField.getByKey(pref.getField()).getFieldSql());
}

Apologies for the Java code, but hopefully you get my point.

Is there an idiomatic way to solve the same problem in PHP? I can think of a number of solutions — nested associative arrays will do the trick — but I’d like to avoid a hackish solution.

  • 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-12T05:16:47+00:00Added an answer on May 12, 2026 at 5:16 am

    I don’t know Java super well, but you can do most of that – just have to do it differently, unless I misunderstand your question.

    Data members on PHP classes can’t have runtime-calculated values, such as new object instances. So, this would not work

    class ReportField
    {
      public static $foo = new ReportField()
    }
    

    Note: final properties are not allowed except on methods

    It’s actually really curious to me that you’re making one class responsible for two things – an object blueprint AND static storage for instances of itself as well.

    Anyway, here’s what I think your code would look like in PHP

    <?php
    
    class ReportField
    {
      public static $store = array();
    
      private
        $key,
        $label,
        $accessLevel,
        $sql;
    
      private function __construct( $key, $label, $accessLevel, $sql )
      {
        $this->key = $key;
        $this->label = $label;
        $this->accessLevel = $accessLevel;
        $this->sql = $sql;
      }
    
      public static function initializeStore()
      {
        if ( empty( self::$store ) )
        {
          self::$store['foo'] = new self( 'foo', 'Foo', 1, 'foo as my_foo' );
          // repeat
        }
      }
    
      public static function getByKey( $key )
      {
        if ( empty( self::$store ) )
        {
          self::initializeStore();
        }
        if ( isset( self::$store[$key] ) )
        {
          return self::$store[$key];
        }
        throw new Exception( __CLASS__ . " instance identified by key $key not found" );
      }
    
      public function getFieldSql()
      {
        return $this->sql;
      }
    }
    
    // Usage
    $selectFields = array();
    foreach ( $reportPrefs as $pref )
    {
      $selectFields[] = ReportField::getByKey( $pref->getField() )->getFieldSql();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have a products table that contains normal products and configurable product It
I am building an e-shop that will have configurable products. The configurable parts will
I have a servlet that needs to write out files that have a user-configurable
I have an entity (layout-template) that will have up to 10 configurable sidebars that
This is tricky to explain. We have a DataTable that contains a user configurable
I have created a custom panel (MyCustomControl) that can contain other controls and be
I have a script that is creating a lot of configurable products and their
I have the usual configurable-simple products store. Ideally I would like that a configurable
I have SCP (simple configurable product) installed and for every simple product that has
I have a GUI app written in C++/CLI which has a load of configurable

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.