Functions 1.0.1 · Library for PHP

The helpers PHP lacks, one function at a time

Functions is a collection of standalone functions in the divengine namespace. Each one does a single thing and works on its own: conversions that take any value and return the type they promise, typed objects composed from decoded JSON, values divided into pieces and joined again, and objects mapped to new shapes.

Free software (GPL 3) · PHP 8.0 or newer · no dependencies

  1. json_decode()a plain object
  2. cop($person, ...)pours it into typed classes
  3. $person->addresses[1]an Address, with its Geo

Examples

What sets it apart

Each example is a complete script, run with Functions 1.0.1. The output is what it printed.

Typed objects from JSON

cop() fills an object from decoded JSON, building nested objects from the property types and array items from their @var docblock.

examples/cop.php
<?php
error_reporting(E_ALL & ~E_DEPRECATED); // Functions 1.0.1 on PHP 8.4+: see the notes
require __DIR__ . '/../vendor/autoload.php';

use function divengine\cop;

// cop() pours decoded JSON into typed objects: nested objects are built from
// the property types, and the items of an array from its @var docblock.
class Geo { public float $lat; public float $lng; }
class Address { public string $city; public Geo $geo; }
class Person
{
    public int $id;
    public string $name;
    /** @var array<\Address> */
    public array $addresses;
}

$json = '{"id": 7, "name": "Ada", "addresses": [
    {"city": "London", "geo": {"lat": 51.5, "lng": -0.12}},
    {"city": "Paris", "geo": {"lat": 48.85, "lng": 2.35}}]}';

$ada = new Person();
cop($ada, json_decode($json));

echo get_class($ada->addresses[1]), ' in ', $ada->addresses[1]->city, "\n";
echo get_class($ada->addresses[1]->geo), ' ', $ada->addresses[1]->geo->lat, "\n";

// Composing again changes only what the complement brings
cop($ada, ['name' => 'Ada Lovelace']);
echo "$ada->name, #$ada->id, ", count($ada->addresses), " addresses\n";
Output
Address in Paris
Geo 48.85
Ada Lovelace, #7, 2 addresses

Conversions that keep their promise

Any value in, the promised type out: no warnings, no surprises with null.

examples/convert.php
<?php
error_reporting(E_ALL & ~E_DEPRECATED); // Functions 1.0.1 on PHP 8.4+: see the notes
require __DIR__ . '/../vendor/autoload.php';

use function divengine\{string, boolean, len, teaser, int_or_default, first_not_empty, remove_accents};

// Conversions that take any value and always return the type they promise
foreach ([42, true, ['a' => 1], null] as $value) {
    echo str_pad(json_encode($value), 10), ' string() => "', string($value), "\"\n";
}

echo "boolean('false'): ", var_export(boolean('false'), true), "\n";
echo "len(12345): ", len(12345), ", len('ñandú'): ", len('ñandú'), "\n";
echo "int_or_default('', 5): ", int_or_default('', 5), ", int_or_default('12', 5): ", int_or_default('12', 5), "\n";
echo 'first_not_empty(null, "", "x"): ', first_not_empty(null, '', 'x'), "\n";
echo "remove_accents('Canción'): ", remove_accents('Canción'), "\n";
echo 'teaser(): ', teaser('Div separates the template from the code that fills it.', 30), "\n";
Output
42         string() => "42"
true       string() => "true"
{"a":1}    string() => "{"a":1}"
null       string() => ""
boolean('false'): false
len(12345): 5, len('ñandú'): 5
int_or_default('', 5): 5, int_or_default('12', 5): 12
first_not_empty(null, "", "x"): x
remove_accents('Canción'): Cancion
teaser(): Div separates the template...

Divide, conquer and map

divide() splits a value into pieces and conquer() joins them; map() renames and computes fields.

examples/divide.php
<?php
error_reporting(E_ALL & ~E_DEPRECATED); // Functions 1.0.1 on PHP 8.4+: see the notes
require __DIR__ . '/../vendor/autoload.php';

use function divengine\{divide, conquer, map};

// divide() splits any value into pieces; conquer() joins pieces into one value
print_r(divide('Div'));
echo 'conquer([1, 2, 3]): ', conquer([1, 2, 3]), "\n";
echo "conquer(['a', 'b']): ", conquer(['a', 'b']), "\n";
echo 'conquer of two objects: ', json_encode(conquer([(object) ['a' => 1], (object) ['b' => 2]])), "\n";

// map() reshapes objects: rename, compute, or set fixed values
$people = [(object) ['first' => 'Ada', 'born' => 1815], (object) ['first' => 'Alan', 'born' => 1912]];
$cards = map($people, [
    'name' => 'first',
    'age_in_2026' => fn ($p) => 2026 - $p->born,
    'kind' => fn () => 'person',
]);
echo json_encode($cards), "\n";
Output
Array
(
    [0] => D
    [1] => i
    [2] => v
)
conquer([1, 2, 3]): 123
conquer(['a', 'b']): ab
conquer of two objects: {"a":1,"b":2}
[{"name":"Ada","age_in_2026":211,"kind":"person"},{"name":"Alan","age_in_2026":114,"kind":"person"}]

Install

Add it to a project

1. Require it

With Composer.

composer require divengine/functions:1.0.1

2. Run an example

Save one of the scripts above in an examples folder of the project, then:

php examples/cop.php

Functions 1.0.1 raises two deprecation notices on PHP 8.4 and newer, which the examples hide on their first line. The examples, with the commands to run them again with Functions 1.0.1, are in the site's repository.