A couple of interesting readings here: What is currying What is dependency injection
Regardless of the programming language or structure, we can say that "Injection of Dependency" (DI) is something like delegation in the definition of an OOP class (see example), and Currying is something completely different, say simplification of function parameters.
DI allows you to maintain simple and maladaptive classes. If you are familiar with Laravel, ServiceProvider is a good example. In a nutshell, your class has a property that will be set to an instance of another class (usually implementing an interface) so that you can delegate some functions. A typical example:
interface LogInterface { public function write(); } class LogToFile implements LogInterface { public function write() {
I just wrote this code above (maybe not working) to illustrate the DI. Using the interface, you guarantee that the "write ()" method will be implemented in any class that implements LogInterface. Then your class forces you to get a LogInterface object when creating objects. Forget how write () works; it's not your business.
Regarding Currying, the technique is different; you simply simplify the parameters in the function. When using the Slim Framework, the guys say, "You can curry your routes using the $ app." I used Slim a lot, and I understand that this is normal, rathaer, than passing multiple variables to your route locks, just enrich the $ app variable and pass 1 one-time var: $ app
$app = new \Slim\Slim(); //Rather than //$app->get('/foo', function () use ($app, $log, $store, ...) { $app->get('/foo', function () use ($app) { $app->render('foo.php'); // <-- SUCCESS $app->log->write(); $app->db->store(); });
source share