2012-04-27 65 views
8

我正在編寫一個應用程序,允許用戶修改和更改其某些網站設置。我只是構建一個表單生成器,將發送各種選項變化插件生成的代碼我想知道的是我應該使用對象而不是多維數組?如果是這樣,我將如何更改我的代碼?PHP對象或數組

所以現在我做了這個 - 它很長,而且會變得更長,所以我只貼一部分簡潔起見: -

$scopeSettings = array(
    'site_background' => array(
     'subpanels' => array(
      'colour' => array(
       'plugins' => array(
        'colourchooser' => array(
         'tip' => "The background colour appears underneath the 'Background Image' (if set)-hover over the '?' around the colour chooser for extra tips on how to use it", 
         'element' => 'body', 
         'gradientenabled' => 'true', 
         'opts' => array (
          'closed' => 'true', 
          'advanced' => array(
           'tip' => "You can paste in your own generated gradient codes in here", 
           'checkbox' => true 
          )//end advanced 
         )//end Opts 
        )//end colour chooser 
       )//end plugins 
      ),//end colour sub panel 
      'pattern' => array(
       'plugins' => array(
        'patternselector' => array(
         'tip' => "Use the pattern selector to apply effects like moire or scan lines to your background image", 
         'element' => 'patimg' 
        )//end patternselector 
       )//end plugins 
      ),//end pattern sub panel 
     )//end subpanels 
    )//end site background 
);//end scope settings 

什麼是有這種最佳實踐的東西?

回答

0

我會說有些像Array Oriented Programmation,並且在PHP 5.4中,他們可以用很好的方式表達自己。 但是,越來越多的人習慣於OOP,所以我也是如此,它可以是一種更可讀的編碼解決方案。

4

也許這是一種愚蠢,但是您可以使用「YAML」或「JSON」作爲應用程序的配置格式否?

例如Symfony或其他框架。

+0

是的,其實聽起來不錯,我喜歡JSON ...;)我可以在php和js之間進行創作,我可能需要做...我想我會嘗試一下... – 2012-04-27 14:45:22

1

我的建議是:嘗試YAML或XML或JSON以獲得更易讀的配置文件,然後將其解析回您自己的代碼中的數組。

0

我認爲在這條路線上我會選擇一個結構化的面向對象的路線。你可以有一個父對象,它包含所有的子對象(設置組),你甚至可以在設置組上檢索它自己的對象。每個對象都有自己的定義和屬性,可以在IDE中提供有用信息的代碼中記錄(如果使用docblock)。

1

我會將設置存儲在<insert your markup language of choice>(XML,JSON,YAML等)中。

然後,您可以在$_SESSION變量緩存這些,並填充它,當你引導,如果他們不已經存在:

session_start(); 

if (!isset($_SESSION['settings'])) { 
    // Assuming you choose JSON... 
    $settings = json_decode(file_get_contents('settings.json'), TRUE); 
    $_SESSION['settings'] = $settings; // array 
    $_SESSION['settings'] = (object)$settings; // object 
} 

無論你是否使用數組或對象,然後變成只是一個事你喜歡什麼訪問語法:

$_SESSION['settings']['site_background']['subpanels']['colour']... 
// vs. 
$_SESSION['settings']->site_background->subpanels->colour... 
0

我會使用對象。假設你是一個形式,你應該有幾類:

  • Form - 持有形式,將有一個屬性$input_list(或$node_list來保存所有的輸入
  • Input - 來描述一個輸入項目,它應該具有的屬性,如labeltypetip
  • Fieldset - 形容一個字段,舉辦更多的項目裏面像Form類,這將有$input_list來保存所有的輸入insid。它的。

這些類可單獨存在,並可以擴展到已經定​​制(例如)

class Checkbox extends Input { 
    public $type = 'checkbox' 
    .... 
} 
0

至於其他的人在這裏也有我的意見是使用YAML或JSON常見的輸入類型 - 利用這一點,可以以非常簡單的方式完成。

就比如你的數據結構的JSON格式的例子:

var settings = { 
    'site_background' : { 
     'subpanels' : { 
      'colour' : { 
       'plugins' : { 
        'colourchooser' : { 
         'tip' : "The background colour appears underneath the 'Background Image' (if set)-hover over the '?' around the colour chooser for extra tips on how to use it", 
         'element' : 'body', 
         'gradientenabled' : 'true', 
         'opts' : { 
          'closed' : 'true', 
          'advanced' : { 
           'tip' : "You can paste in your own generated gradient codes in here", 
           'checkbox' : true 
          }//end advanced 
         }//end Opts 
        }//end colour chooser 
       }//end plugins 
      },//end colour sub panel 
      'pattern' : { 
       'plugins' : { 
        'patternselector' : { 
         'tip' : "Use the pattern selector to apply effects like moire or scan lines to your background image", 
         'element' : 'patimg' 
        }//end patternselector 
       }//end plugins 
      }//end pattern sub panel 
     }//end subpanels 
    }//end site background 
};//end scope 

您可以使用PHP函數像json_encode和json_decode的JSON < - > PHP數據轉換。使用大括號意味着元素是對象,而當[...]替換爲數組時...

但是也可以成功使用PHP OOP方法,特別是在使用可擴展性時。您可以讓一個主類設置具有一些默認屬性,例如magic __get和__set函數,然後您可以實現許多從這個主要Settings類延伸的子集。