Welcome Guest, Not a member yet? Register   Sign In
Functional PHP Extension
#21

[eluser]Jamongkad[/eluser]
[quote author="sophistry" date="1215583340"]so... Jamongkad... when you are feeling better... can you drop some super (applied) science on us and give a real-world example of how you use this library in your CI development process?

Maybe you have something that demonstrates an FP concept that is a little more (ahem) robust than string concatenation?

much thx :-)[/quote]

Haha no problem Sophistry, I'm sorry for the delay but to answer your question. I have to admit I don't have any super (applied) science stuff (as of yet) that I do with the library in my CI development process. It's really a supplement and a constant aid to help express my code in more idiomatic ways. Anyways I'm more than happy to show the community how to use the library.

Whenever I have the chance I'm such a big fan of the magic method __call() I use it whenever I see that the design of the program needs it. Let's say I want to invoke a method in my model by using call...

Taking a page off of the excellent ActiveRecord ORM created by Buddy...
Code:
function __call($method,$args) {
  if(stristr($method,'whatever_method'))
   //Apply is similar to call_user_func_array but without the long function name.
   //qoute is flexible enough to use as an alternative array creator.
   //concat takes an array as a argument and returns a string.
   return apply( array($this,'targetFunctionWithinModel'), qoute(concat($args)) );
}

  //or better yet we use the qoute and unqoute Scheme like mechanisms do the same thing
  function __call($method,$args) {
  if(stristr($method,'whatever_method'))
   return unqoute( qoute( ($this,'targetFunctionWithinModel'), concat($args) ) );
}

New

Detailed Explanation
An important note of discussion to this approach is that we could have easily used the Foreach construct of PHP to simply iterate through the arguments and return the function call(think map function in Python to graft functions to other functions). But instead we used the FP approach of calling the function and applying the arguments without the need of a looping construct. Such approach as seen above is much more elegant in composition and results to short programs.




Theme © iAndrew 2016 - Forum software by © MyBB