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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题