Search⌘ K

Implementation of Registry Functionalities

Explore the implementation of registry functionalities in a PHP phonebook application using functional programming principles. Understand how to use IO monads for file operations, compose create, delete, and search functions, and format output for clear data display. This lesson guides you through handling a flat-file registry with JSON data, enabling you to build a functional and composable contact management system.

Registry functions in actions.php

It’s impossible to complete the application without factoring in the registry-related functionalities that correspond to the other more sophisticated patterns in the table, that is, interaction with a registry and, therefore, the filesystem. Working with a flat-file registry, or any file for that matter, means that many of the actions performed on the said entity are some variation of read and write operations, or sometimes a mix of both.

Read and write are impure operations, much like printing to a standard output device (a console) and, as such, warrant the usage of the IO monad. The mostly empty bodies of the functions defined in the snippet below are imperative to realizing a fully functioning phonebook.

PHP
<?php
function writeRegistry(callable $action): IO
{
// write changes to registry
}
function create(string $name, string $phone): IO
{
// create a contact
}
function delete(string $name): IO
{
// delete a contact
}
function search(string $search): IO
{
// search registry
}
function readRegistry(): IO
{
// read registry contents
}
function registry(): string
{
return f\filePath(0, REGISTRY_FILE);
}

The readRegistry and writeRegistry functions are especially useful. The former makes it possible to read from the registry, and the latter allows for write-predicated actions. They’re both written to be composable with create ...