0

I recently faced a design problem with PHP. I noticed that in a function you can pass as parameter an array. I didn't noticed the powerful of this thing first, but now i'm obsessed with arrays.

For example, in my template class i have to pass some variables and some mysqli_results into the template file (like phpbb do). And i was wondering which one of the following possibilities is the best.

# 1
$tpl = new template(array(
    'vars' = array('var1' => 'val1', 'var2' => 'val2'),
    'loops' = array('loop1' => $result1, 'loop2' => $result2)
));

# 2
$tpl = new template;
$tpl->assignVars(array(
    'var1' => 'val1', 
    'var2' => 'val2'
));
$tpl->assignloops(array(
    'loop1' => $result1,
    'loop2' => $result2
));

# 3
$tpl = new template;
$tpl->assignVar('var1', 'val1');
$tpl->assignVar('var1', 'val1');
$tpl->assignLoop('loop1', $result1);
$tpl->assignLoop('loop2', $result2);

Or if there is something better. I was even thinking about creating a db class that performs a query as follow:

$result = $db->fastQuery(array(
        'select' => 'user-name',
        'from' => $table,
        'where' => array('user-id' => 123, 'user-image' => 'none'),
        'fetch' => true
    ));

Oh my God, i'm really obsessed.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 4
    You're obsessed with arrays for this? Then you are going to *love* [chaining](http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html). – Pekka Dec 30 '10 at 18:59
  • Related: http://stackoverflow.com/questions/2112913/should-my-php-functions-accept-an-array-of-arguments-or-should-i-explicitly-reque – Mike B Dec 30 '10 at 18:59
  • Does this means all of them are good examples? – Shoe Dec 30 '10 at 18:59
  • @Mike B, it's slightly different. – Shoe Dec 30 '10 at 19:00
  • I didn't say it was a dupe, just referencing a similar discussion :) – Mike B Dec 30 '10 at 19:01

2 Answers2

1

#4

Allowing both:

function assign($name, $val = null)
{
    if (is_array($name)) {
        // loop through and assign
    } else {
        // assign single var
    }
}

This is akin to overloading techniques you would see in C++/Java.

You can also allow #1 by just calling assign in the constructor. It is not uncommon in OOP programming to have the constructor allow a shortcut to setting properties that can also be set in other methods.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
1

If it was up to me I would chose #1, I don't like nesting objects and arrays only if it is necessary. by doing so I keep my code simple. and if you follow your obsession you may end up writing a full ORM.

Ayoub M.
  • 4,690
  • 10
  • 42
  • 52
  • Sorry, what is a "full ORM"? I googled it and it brings me into 2 different programming paths. – Shoe Dec 30 '10 at 19:12
  • "Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages." wikipedia. usually ORMs do convert sql data into objects and it take in charge the sql relations without much code – Ayoub M. Dec 30 '10 at 19:16