8

I am writing an app that allows users to modify and change some of their site settings. I am just constructing a form generator that will send various options to variuous plugins to generate the code what I am wondering is whether I should be using objects for this rather than multidimensional arrays? If so how would I change my code?

So right now I have made this- its very long and going to get longer so I have only pasted part of it for the sake of brevity:-

$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 practice with this sort of thing?

7 Answers 7

4

Maybe this is a stupidity, but you can use "YAML" or "JSON" as configuration format for an application no?

As for example Symfony or other framework.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah actually that sounds good, I like JSON...;) And I can wang it between php and js then too, which I may need to do... I think I'll try that...
1

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

Comments

1

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

You can then cache these in a $_SESSION variable, and populate it when you bootstrap if they don't 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
}

Whether or not you use an array or object then becomes just a matter of what access syntax you prefer:

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

Comments

0

I would say that some like Array Oriented Programmation and that, with PHP 5.4 they can express themselves in a great way. However, more people are used to OOP -so do I- and It can be a more readable way to code your solution.

Comments

0

I think in this route I would go with a structured object oriented route. You can have a parent object, that holds all of the children objects (setting groups) and you can even retrieve on setting group as an object of its own. Each object will have its own definitions and properties which can be documented in the code that provides useful information within an IDE (if you use docblocks).

Comments

0

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

  • Form - to hold the form, will have a property $input_list (or $node_list to hold all of the inputs
  • Input - to describe a single input item, it should have properties like label, type, tip etc.
  • Fieldset - to describe a fieldset, to hold additional items inside. Like the Form class, it would have an $input_list to hold all of the inputs inside of it.

Those classes can stand alone, and can be extended to have customized common input types (for instance)

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

Comments

0

As other's people here also my opinion is to use YAML or JSON - using this it could be done in very simple way.

Just for instance an example of JSON format of 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 for JSON <-> PHP data transformation. Using curly braces means that the elements are objects while when replaced by [ and ] you got arrays...

But also a PHP OOP approach could be used succesfully especially when using extendability. You could have one main class Settings having some default properties and e.g. magic __get and __set functions and then You can implement many subsettings subclasses extending from this main Settings class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.