Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <em>every class</em> should have the colors, define the interface that allows to get them:</p> <pre><code>interface ColorsProvider { function getColors(); } class FirstScheme implements ColorsProvider { public static COLORS = array('1' =&gt; 'green', '2' =&gt; 'red', ...); public function getColors() { return self::COLORS; } } class SecondScheme implements ColorsProvider { public static COLORS = array('1' =&gt; 'red', '2' =&gt; 'green', ...); public function getColors() { return self::COLORS; } } </code></pre> <p>Then, where you have stack of yout params:</p> <pre><code>$a = array( 'attr_value1' =&gt; new FirstScheme(), 'attr_value2' =&gt; new SecondScheme(), ); </code></pre> <p>You can call:</p> <pre><code>$param = 'attr_value1'; if(!isset($a[$param])) throw new Exception("undefined param"); if(!($a[$param] instanceof ColorsProvider)) throw new Exception("Param should point to ColorsProvider"); $a[$param]-&gt;getColors(); </code></pre> <hr> <p>Please note that it is full-objective. In PHP there are simplier ways to get this effects, but my solution is just elegant.</p> <hr> <p>The another point is the interface <em>completely separates the source</em> of colors. There would be from file, database, xml, hardcoded etc.</p> <hr> <p>Default implementation might be:</p> <pre><code>abstract class DefaultColorsProviderImpl implements ColorsProvider { protected static COLORS = array(); public function getColors() { return self::COLORS; } } class FirstScheme extends DefaultColorsProviderImpl { protected static COLORS = array( ... ); } </code></pre> <p>But still allows to make generic implementation that returns colors from e.x. from file.</p>
    singulars
    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.
    3. 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