PHP object or array

I am writing an application that allows users to modify and change some parameters of their site. I'm just creating a form generator that will send different options for different plugins to generate code, which is interesting to me, should I use objects for this, and not for multidimensional arrays? If so, how can I change my code?

So, now I have done this - for a very long time and am going to take longer, so for the sake of brevity I have only enclosed part of it: -

$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 

What would be best with this?

+8
object arrays php
source share
7 answers

This may be silly, but can you use "YAML" or "JSON" as the configuration format for application no?

Like, for example, Symfony or other infrastructure.

+4
source share

My advice: try YAML or XML or JSON to get a more readable configuration file, then parse it back into an array in your own code.

+1
source share

I would save the settings in <insert your markup language of choice> (XML, JSON, YAML, etc.).

You can then cache them in the $_SESSION variable and populate it at boot if they do not already exist:

 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 } 

Regardless of whether you use an array or an object, you simply decide which access syntax you prefer:

 $_SESSION['settings']['site_background']['subpanels']['colour']... // vs. $_SESSION['settings']->site_background->subpanels->colour... 
+1
source share

I would say that some are like the Array Oriented Programmation, and that with PHP 5.4 they can express themselves superbly. However, more people are used for OOP - also me - and this may be a more readable way to code your solution.

0
source share

I think that in this route I would go with a structured object-oriented route. You can have a parent object that stores all the child objects (setting groups), and you can even retrieve the settings group as your own object. Each object will have its own definitions and properties, which can be documented in code that provides useful information in the IDE (if you use dockblocks).

0
source share

I would use objects. Assuming you are making a form, you should have several classes:

  • Form - to save the form, it will have the property $input_list (or $node_list to store all inputs
  • Input - to describe a single input element, it must have properties such as label , type , tip , etc.
  • Fieldset - describe a set of fields to hold additional elements inside. Like the Form class, it must have $input_list to store all the input data inside it.

These classes can stand alone and can be extended to have individual common input types (for example)

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

Like other people, I also believe that using YAML or JSON can be done in a very simple way.

Just an example of an example JSON format for your data structure:

 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 

You can use PHP functions like json_encode and json_decode to convert JSON ↔ PHP data. Using curly braces means that the elements are objects, and when replaced with [and] you got arrays ...

But also the PHP OOP approach can be used successfully, especially when using extensibility. You can have one main class of settings that has some default properties and, for example, magic __get and __set, and then you can implement many sub-classes of sub-settings that extend from this main class of settings.

0
source share

All Articles