Peter Stuifzand

One

In the article about why we don’t use more data structures in web applications I showed an example of PHP code. I copied the code below. The example shows how we start to write web applications. I will rewrite the code so it says one thing: insert a person into the database.

<?php
    $name    = $_POST['name'];
    $address = $_POST['street'];
    $city    = $_POST['city'];
    db_person_insert($db, $name, $address, $city);
?>

Two things happen in this example. First we get the variables from the $_POST variable. And then we insert the values into the database. First I’ll add the ‘code’ of the db_person_insert function as well.

<?php
function db_person_insert($db, $name, $address, $city) {
    // some SQL code...
}
?>

The function accepts four parameters: one database connection, a name, an address and a city. The last three arguments are related because they talk about the same person. The code however doesn’t show us that. Let’s make a change so it does.

<?php
function db_person_insert($db, $person) {
    // some SQL code...
    // use $person['name'], $person['address'] and $person['city']
}
?>

The interface of the function is much cleaner now and doesn’t have to change when we change what data we want to know about people. In the previous article I showed what happened when we added the phone number. Now only the implementation of the function needs to change.

Let’s go back to the first example. This code has to change to work with the new db_person_insert function.

<?php
    $name    = $_POST['name'];
    $address = $_POST['street'];
    $city    = $_POST['city'];
    db_person_insert($db,
        array('name' => $name, 'address' => $address, 'city' => $city)
    );
?>

This code is already better, because it shows that there is only one argument that is used as a whole. But there is another change we can make, the three variables at the top refer to the same person. Why don’t we have one variable representing that person?

<?php
    $name    = $_POST['name'];
    $address = $_POST['street'];
    $city    = $_POST['city'];
    $person = array(
      'name'    => $name,
      'address' => $address,
      'city'    => $city
    )
    db_person_insert($db, $person);
?>

The structure of the program becomes a bit clearer again. Let’s take it one step further and create a function that gets the values from the post variable.

<?php
    $person = person_from_postarray($_POST);
    db_person_insert($db, $person);

    function person_from_postarray($arr) {
        $name    = $arr['name'];
        $address = $arr['street'];
        $city    = $arr['city'];
        $person = array(
          'name'    => $name,
          'address' => $address,
          'city'    => $city
        )
        return $person;
    }
?>

This still isn’t the best version of the code, because there are still a few pieces of duplication. The person_from_postarray function is a bit sad, because it only makes a copy of particalur fields from $arr to the new array $person. So lets rename the function.

function person_copy($arr) {
    $name    = $arr['name'];
    $address = $arr['street'];
    $city    = $arr['city'];
    $person = array(
      'name'    => $name,
      'address' => $address,
      'city'    => $city
    )
    return $person;
}

This piece of code is more general than the previous version, because it can be used for more problems, while it still makes sense. Now we make the change to make it more like a function that copies a person. The first step.

function person_copy($arr) {
    $person = array();
    $person['name'] = $arr['name'];
    $person['address'] = $arr['street'];
    $person['city'] = $arr['city'];
    return $person;
}

This makes the structure very obvious, but there is one field that’s problematic, the street or address field. The nice thing about where we’re going what this, is that we can make our lives simpler by making the fields of the person the same everywhere. So one thing we could do would be to change the street field to the address field, or the other way around. The solution depends on what you can change. Let’s say we name the field ‘address’ and change the web interface.

function person_copy($arr) {
    $person = array();
    foreach (array('name', 'address', 'city') as $field) {
        $person[$field] = $arr[$field];
    }
    return $person;
}

Done! This function now creates a new person based on values from another array. By representing a person as one thing, we can simplify the code that works with it.

© 2023 Peter Stuifzand