Search⌘ K

Building the Phonebook App

Explore how to create a functional phonebook application in PHP. Understand the project structure, use Composer for autoloading, manage immutable state, and implement command parsing to handle user interactions effectively.

Let’s discuss the role of different files from the project’s file structure.

The composer.json file

Now, for the project’s configuration, the first step is to create a composer.json file that includes the project dependencies and autoload constraints.

Javascript (babel-node)
{
"require": {
"chemem/bingo-functional": "~1",
"mmarica/display-table": "~1"
},
"autoload": {
"psr-4": {
"Project\\": "src/"
},
"files": [
"src/constants.php",
"src/patterns.php",
"src/actions.php"
]
}
}

Composer automatically adds all source files to the autoloader it generates. The autoloader is normally included at the top of entry scripts. There is, therefore, no need for a custom autoloader script.

The registry.json file

It’s a simple JSON name and phone number value store to which all contacts are saved and deleted at the user’s discretion. Ordinarily, we might store the key-value data in a proper database, but the simplicity of the phonebook means that a flat-file standard will suffice.

JavaScript (JSX)
[
{
"name": "Sheridan",
"phone": "+256 725523656"
},
{
"name": "Alex",
"phone": "+256 786321421"
},
{
"name": "Jeffrey",
"phone": "+256 758789845"
},
{
"name": "Michael",
"phone": "+256 897812212"
}
]

Immutable state is a good thing to have. The second chapter of this course emphasized this. PHP does not have organic support for immutable values beyond constants limited to array and non-composite primitive bindings. An ...