10

This is probably considered a really silly question, but I'm in the process of putting together a simple template system for a website and am trying to keep track of my variable usage and keep everything neat and tidy.

Can you tell me if there is any advantage/disadvantage to the following methods:

simple var:

$tpl_title = 'my title'
$tpl_desc = 'my text'

array:

$tpl['title'] = 'my title'
$tpl['desc'] = 'my text'

Object:

$tpl->title = 'my title'
$tpl->desc = 'my text'

I like the object method the best as it looks clean when echo'd within html as opposed to arrays and afaik it can be used in an array-like way? However, what I want to know is whether using objects in this way is considered bad practice or introduces unneccesary overheads?

George
  • 36,413
  • 9
  • 66
  • 103
James
  • 656
  • 2
  • 10
  • 24
  • Its your own personal choice how to set/use your data.. There is no difference if you use vars/array/object. Its important to be comfortable to you.. – Svetoslav Mar 20 '13 at 10:57
  • Use `array` or `object` in `json` format .. this way your html can work in multiple languages – Baba Mar 20 '13 at 10:57
  • http://stackoverflow.com/questions/2193049/php-objects-vs-arrays :) – Terry Seidler Mar 20 '13 at 10:58

7 Answers7

8

In ideal scenarios every variable should belong to an object, other than the variables local to methods for temp purposes. However we don't live in an ideal world and specially our programming languages are far from it. Based on what the situation is, choose the best way to go about it to make your life easier. If you are using things for templates, generally you keep all the data in an array and extract the array to get stand alone variables.

So yeah, the object method is the nicest, try to make it happen as much as you can without spending crazy amounts of time in doing it.

Also if you love objects and want to have the neat -> way of doing it, you can do

$object = (object)$array;

That would convert your array to an object.

Hope that helps.

Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50
6

I would consider it unnecessary overhead. Doing what you are talking about in an object-oriented way only means that inside your class you will have done nothing more than create a bunch of variables, just as you specified in your first example.

The array is the best way to go in my opinion. You are only using one variable, and you can also integrate it into your Class. So, instead of $tpl->title, you may have $tpl->text['title'].

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
dockeryZ
  • 3,981
  • 1
  • 20
  • 28
  • 2
    When talking about overhead and array vs objects mind these things: a) With PHP 5.0 objects became reference types, this avoids copies even in cases which aren't handled by PHP's "normal" copy-on-write b) With PHP 5.4 properties defined in classes aren't stored in a hash table but a C array, this (combined with 5.3's interned strings etc.) makes accss faster and needs less memory c) on the other side for "normal" variables we have "compiled variables" since 5.1 ... essence: Argueing iwth "overhead" on this level is complicated and depends on the exact things being done and the PHP version – johannes Mar 20 '13 at 12:01
  • 2
    In other words: Write maintainable and understandable code, unless you know you have an performance issue ... – johannes Mar 20 '13 at 12:13
  • Array access style is also saver, consider this: `$o['stupid.key[]']` works for setting and retrieving the value, but trying to access a value like that with `$o->stupid.key[]` will fail. – maraca Jun 16 '15 at 18:30
2

Arrays

I would suggest on the backend part, keep everything stored in an array. This allows you to have only one variable to keep track of, and once you pass it to the frontend, you can extract() the array, to convert them into simple variables.

Syntax

Using extract() simplifies the syntax on the FrontEnd, which means you will only always have $varibles in the template.

On the backend you would set
$array['title'];

Which once extracted would in the template be
$title;

Example of a backend function

 protected function fetch($template, $data = null)
    {
        if (!$this->isTemplate($template)) {
            throw new Exception("Template file $this->_templatePath$template not found");
        }
        ob_start();
        if (is_array($data)) {
            extract($data, EXTR_SKIP);
        }
        require $this->_templatePath . $template . EXT;
        return ob_get_clean();
    }
Kao
  • 2,242
  • 3
  • 22
  • 31
0

In the end they are the same, It depends on the preference, although I would use arrays or objects because you can group variables in there, so you have things better sorted out.

Despite the objects method works I think it's not the natural intended use for it.

So I would say arrays!

Also, there are tons of php native functions you can use with arrays, like array_map() or array_filter() array sortings and etc etc...

aleation
  • 4,796
  • 1
  • 21
  • 35
0

In my opinion the cleanest way to set it up is in array like this:

$tpl = array (
  'title' => 'my title',
  'desc' => 'my text'
);

You can combine it with Zane's answer also.

All the best!

Goran
  • 3,292
  • 2
  • 29
  • 32
0
$tpl = array (
  'title' => 'my title',
  'desc' => 'my text'
);

Like Goran said, with the bonus that you could store these arrays in an ini file later and extract them as needed with parse_ini_file

Could be important if you want to permit users to write to their own ini file.

Cups
  • 6,901
  • 3
  • 26
  • 30
0

arrays and objects are useful in case if you want to reset bulk of variable data after use it.

you can simply unset the array or destroy the object instead of unset range of variables use in a code.

Other than this arrays and objects have more symmetry in code and explanatory than normal variables

Hasan Baig
  • 491
  • 6
  • 17