Registers a PHP callback callable from SQL statements in that SQLite connection.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Registers a PHP callback callable from SQL statements in that SQLite connection.
Syntax and Return Value
Pdo\Sqlite::createFunction(...)
The return value and exceptional cases depend on the call described here: Return an SQLite-compatible scalar and never execute untrusted callbacks.
Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.
The driver-specific PDO subclasses are available starting with PHP 8.4, except setAuthorizer, which starts with PHP 8.5.
Example 1: Basic Use
The callback runs on SQLite values and returns a scalar number to SQL.
<?php
$sqlite = PDO::connect("sqlite::memory:");
$sqlite->exec("CREATE TABLE scores (name TEXT, points INTEGER)");
$sqlite->exec("INSERT INTO scores VALUES ('Ava',10),('Ben',20),('Cai',30)");
$sqlite->createFunction("bonus", static fn(int $points): int => $points + 5, 1);
echo $sqlite->query("SELECT bonus(points) FROM scores WHERE name = 'Ava'")->fetchColumn();
Example 2: A Second Scenario
The callback runs on SQLite values and returns a scalar number to SQL.
<?php
$sqlite = PDO::connect("sqlite::memory:");
$sqlite->exec("CREATE TABLE scores (name TEXT, points INTEGER)");
$sqlite->exec("INSERT INTO scores VALUES ('Ava',10),('Ben',20),('Cai',30)");
$sqlite->createFunction("bonus", static fn(int $points): int => $points + 6, 1);
echo $sqlite->query("SELECT bonus(points) FROM scores WHERE name = 'Ava'")->fetchColumn();
Example 3: Practical Checks
The callback runs on SQLite values and returns a scalar number to SQL.
<?php
$sqlite = PDO::connect("sqlite::memory:");
$sqlite->exec("CREATE TABLE scores (name TEXT, points INTEGER)");
$sqlite->exec("INSERT INTO scores VALUES ('Ava',10),('Ben',20),('Cai',30)");
$sqlite->createFunction("bonus", static fn(int $points): int => $points + 7, 1);
echo $sqlite->query("SELECT bonus(points) FROM scores WHERE name = 'Ava'")->fetchColumn();
Important Behavior and Edge Cases
Return an SQLite-compatible scalar and never execute untrusted callbacks.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| Pdo\Sqlite::createFunction | Registers a PHP callback callable from SQL statements in that SQLite connection. |
| Related API / pattern | createAggregate combines values from several rows rather than processing one call at a time. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Return an SQLite-compatible scalar and never execute untrusted callbacks. |
| Using a related API interchangeably | createAggregate combines values from several rows rather than processing one call at a time. |
Key Takeaways
- Registers a PHP callback callable from SQL statements in that SQLite connection.
- Return an SQLite-compatible scalar and never execute untrusted callbacks.
- createAggregate combines values from several rows rather than processing one call at a time.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.