Updated design pattern collection

I guess I'm on the down form my design pattern tantrum hit. I updated the collection so it now has about 20 of them, not all can be considered "officially" design patterns, but to be honest I don't really care. I'm very well aware that I ignore the existance of boundaries between design patterns, idioms, techniques, ... That is intentional. I like my rainbow coloured world and I want to keep it that way. If you disagree - just read the small script. My tanrum energy is over and I need to do work work. So the combing over the php design pattern collection will come later. For now enjoy, and have anger fits of your own.

Oh yes. I'm using a particular case of wishful thinking - using any php callable in $callable(...) expressions. The reality is a bit uglier. We need to use either call_user_func($callable,...); call_user_func_array($callable,...) How I wish php could do that out of the box, especially when it actually does it, just not in the short syntax. Anyone happy to hold my hand to do a php patch for that feature? I'm very unfamiliar with the internals, sorry.

Calling functions/methods with $

Hey vlado, you can indeed call functions or methods by using a variable name. For example:

$myFunction = "doSomething";

$result = $myFunction("arg1", $arg2, etc.)

Or:

$myMethod="eat";
$yummy = $food->$myMethod();

The reason to use call_user_func_array is so you can pass a single array of values that gets translated to multiple arguments.

indeed you can

Yes you can do: $myMethod="eat"; $yummy = $food->$myMethod();

But it will require more logic, and maintaining more variables if you are returning the value of type "thing to be called". There is distinction between function and method calls, so we either need to do explicit type checking or write discriminate against either functions or methods in 'generic' code.

For example: function &example_choice( &$object, $data) { if(some_condition_met($data)) { return array($object, 'met'); } else { return array($object, 'unmet'); } } //now $obj = new someclass(); list($object, $method) = example_choice( $obj, $some_data); $object->$method(); //if we could use callable directly $call = example_choice( $obj, $some_data); $call();

The immediate difference is the abount of data we need to carry around, and maintain. If you look deeper the second syntax enables you to mix function and object calls, have proper first-class functions, that is not only assign their symbols (names) to variables, but clone, etc...

Powered by Drupal, an open source content management system