Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP object or an array

Tags:

arrays

object

php

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?

like image 243
David O'Sullivan Avatar asked Apr 27 '12 14:04

David O'Sullivan


People also ask

Should I use object or array?

Think about what your particular data represents: If it's a single entity with named properties, you want an object. If it's a group of entities of the same type/shape, or if order matters, you likely want an array.

Which is faster array or object in PHP?

The difference between an array numeric and an associative array is a mere 5%, so you can say that they are the same. The use of an object is +100% slower but it is still acceptable in most conditions (aka it uses the double of time).

Why is it better to use objects instead of arrays?

Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.

How can you tell if an element is an array or an object?

Answer: Use the Array. isArray() Method You can use the JavaScript Array. isArray() method to check whether an object (or a variable) is an array or not. This method returns true if the value is an array; otherwise returns false .


1 Answers

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.

like image 74
ke20 Avatar answered Sep 29 '22 07:09

ke20