User Tools

Site Tools


z_appendix_101:php_functions_in_the_ai_era

5. Functions — Small Units of Behavior

Functions are the smallest reusable pieces of behavior in PHP.
They take input, do something, and return a result.
They let us name an idea, give it a shape, and use it anywhere.

Where arrays and objects describe data,
functions describe behavior
.


1. What a Function Looks Like

A simple function:

function double(int $x): int {
    return $x * 2;
}

We give it a name, define its inputs, and return a value.
Functions are small, predictable, and intentional.


2. Why Functions Matter

Functions let us:

  • name a piece of behavior
  • avoid repeating ourself
  • separate “what” from “how”
  • make code easier to read
  • make code easier to test
  • express intention clearly

A good function is a small, well‑named idea.


3. Functions vs Methods

A function stands alone:

function slugify(string $text): string { ... }

A method belongs to an object:

class User {
    public function isAdult(): bool { ... }
}

Both express behavior.
The difference is where the behavior lives.

If the behavior belongs to a concept → method
If the behavior is general → function

The terminology infers where meaning belongs.


4. Small Functions Are Clear Functions

A function should do one thing.
Not two.
Not “mostly one thing, plus a little extra”.

Just one…

function calculateTotal(array $items): float { ... }

If the function starts branching, looping, or juggling too many ideas,
it's a sign the behavior deserves to be broken down
— or moved into an object.

Smallness is clarity.


5. Parameters and Return Values

Functions communicate through:

  • parameters — what they need
  • return values — what they produce

Clear functions have clear signatures:

function greet(string $name): string {
    return "Hello, $name!";
}

The signature tells the story.


6. Pure Functions — Behavior Without Side Effects

A pure function:

  • depends only on its inputs
  • changes nothing outside itself
  • always returns the same output for the same input

Example:

function add(int $a, int $b): int {
    return $a + $b;
}

Pure functions are predictable.
They're easy to test.
They're easy for AI to reason about.

They are the quiet backbone of clean code.


7. When Functions Are the Right Tool

Use a function when the behavior:

  • is small
  • is reusable
  • doesn't belong to a specific object
  • doesn't need state
  • expresses a single idea

Functions are the verbs of our system.


8. When Functions Are Not Enough

Avoid standalone functions when:

  • the behavior depends on data with meaning
  • the behavior belongs to a concept
  • the function needs access to internal rules
  • the function manipulates a domain model

In those cases, the behavior belongs inside the object.

Objects carry meaning.
Functions carry behavior
.


9. Short Closures — Tiny Inline Functions

Modern PHP gives us short closures:

$double = fn(int $x) => $x * 2;

They're perfect for small, single‑expression behavior
— especially in array functions like map, filter, and reduce.

Short closures are for tiny ideas.
Functions are for named ideas
.


The Mental Model

Functions are small units of behavior.
They let us name what our code does.

Use them when:

  • the behavior is simple
  • the behavior is reusable
  • the behavior stands alone
  • the behavior expresses a single intention

Functions make behavior visible.
Objects make meaning visible.
Arrays make values visible
.

Together, they form the basic shapes of PHP.


Summary

This page continues the gentle introduction to modern PHP.
Functions are not about abstraction — they are about clarity.
When a piece of behavior starts to repeat,
or when naming it makes our code read more naturally,
give it a function.


Tony de Araujo —New York



Small names, small behaviors, clear code.

z_appendix_101/php_functions_in_the_ai_era.txt · Last modified: by editor