PHP Classes

Why so complicated

Recommend this page to a friend!

      Named Parameters  >  All threads  >  Why so complicated  >  (Un) Subscribe thread alerts  
Subject:Why so complicated
Summary:Use assoc array parameters instead
Messages:5
Author:Kai Dorschner
Date:2011-09-07 09:17:15
Update:2011-09-08 10:43:50
 

  1. Why so complicated   Reply   Report abuse  
Picture of Kai Dorschner Kai Dorschner - 2011-09-07 09:17:15
You could just use associative arrays, as its already a common usage (like in the zend fw).

function myFunc(array $params= array()) {
if(isset($params['arg1'])) doStuff();
}

This needs no extra syntax and extra classes.
So, why so complicated?

Krnl

  2. Re: Why so complicated   Reply   Report abuse  
Picture of Kai Dorschner Kai Dorschner - 2011-09-07 09:21:08 - In reply to message 1 from Kai Dorschner
Oh i forgot. The method call may look like this:
MyFunc(array('arg1' => 'foo', 'ignored' => 'bar'));

  3. Re: Why so complicated   Reply   Report abuse  
Picture of Stefan Jibrail Froelich Stefan Jibrail Froelich - 2011-09-07 16:32:00 - In reply to message 1 from Kai Dorschner
a different approach to the same issue.
With your solution, the function would need to be built that way.
This package is just a fancier way to do that.
Argueably, using arrays for the class calling would be faster.

  4. Re: Why so complicated   Reply   Report abuse  
Picture of Kai Dorschner Kai Dorschner - 2011-09-08 10:39:46 - In reply to message 3 from Stefan Jibrail Froelich
Not only the call would be faster, you can use my mentioned example in another class. Yours not. Zend did this on purpose, otherwise they would have found a similar solution like yours. You might also extend functions with array parameters:

cass Foo
{
function foo(array $params = array())
{
if(isset($params['foo']) $this->doStuff();
}
protected function doStuff()
{
echo 'stuff'.PHP_EOL;
}
}
class Bar
{
function foo(array $params = array())
{
parent::foo($params);
if(isset($params['bar']) $this->doMoreStuff();
}
protected function doMoreStuff()
{
echo 'more stuff'.PHP_EOL;
}
}

$x = new Bar();
$x->foo(array('foo' => 'john', 'bar' => 'doe'));

//Output:
/*
stuff
more stuff
*/

  5. Re: Why so complicated   Reply   Report abuse  
Picture of Kai Dorschner Kai Dorschner - 2011-09-08 10:43:50 - In reply to message 4 from Kai Dorschner
class Bar (of course) extends Foo ;)