Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Data members on PHP classes can't have runtime-calculated values, such as new object instances. So, this would not work</p> <pre><code>class ReportField { public static $foo = new ReportField() } </code></pre> <p><em>Note: <code>final</code> properties are not allowed except on methods</em></p> <p>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.</p> <p>Anyway, here's what I <em>think</em> your code would look like in PHP</p> <pre><code>&lt;?php class ReportField { public static $store = array(); private $key, $label, $accessLevel, $sql; private function __construct( $key, $label, $accessLevel, $sql ) { $this-&gt;key = $key; $this-&gt;label = $label; $this-&gt;accessLevel = $accessLevel; $this-&gt;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-&gt;sql; } } // Usage $selectFields = array(); foreach ( $reportPrefs as $pref ) { $selectFields[] = ReportField::getByKey( $pref-&gt;getField() )-&gt;getFieldSql(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload