douqian4411 2009-07-13 14:05
浏览 21
已采纳

用于创建静态值对象列表的PHP解决方案

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.

  • 写回答

3条回答 默认 最新

  • dtjzpg5313 2009-07-13 14:38
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决