Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Currying Example in PHP
<?php
function get_mail_client()
{
// fictional mail client
return array(
'isValid' => true
);
}
function get_mail_addresses()
{
// fetching users from db or something
return array(
'abcd@example.com',
'efgh@example.com'
);
}
function get_prepared_promotional_email_contents()
{
// fetching the "emails to be sent" from db or something
return array(
array(
'subject' => 'A lucrative subject!',
'content' => 'This email is about a lucrative promotional offer!'
),
array(
'subject' => 'A lame subject!',
'content' => 'This is about a lame promotional offer which will defenitely flop!'
),
);
}
function init_mailing($mail_client)
{
return function ($prepared_mail_contents) use ($mail_client) {
return function ($mail_address) use ($mail_client, $prepared_mail_contents) {
if ($mail_client['isValid']) {
echo "sending an email saying, \"" .
$prepared_mail_contents['content'] .
"\", with subject \"" .
$prepared_mail_contents['subject'] .
"\", to the address \"$mail_address\"";
echo "<br />";
}
};
};
}
// execution starts here...
$mail_client = get_mail_client();
$mail_addresses = get_mail_addresses();
$promotional_email_contents = get_prepared_promotional_email_contents();
$add_email_contents = init_mailing($mail_client);
foreach ($promotional_email_contents as $email_content) {
$send_mail_to = $add_email_contents($email_content);
foreach ($mail_addresses as $address)
$send_mail_to($address);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment