PHP-ruler – strict rule engine in pure PHP, zero deps, explain mode logo

PHP-ruler – strict rule engine in pure PHP, zero deps, explain mode

Free
FreeFree tier
Type
Open Source

About PHP-ruler – strict rule engine in pure PHP, zero deps, explain mode

PHP-ruler is an open-source rule engine that evaluates business rules stored as strings, with strict typing, zero dependencies, and a unique explain mode that traces node-by-node why a rule passed or failed. It allows developers to move business logic (pricing, eligibility, feature flags, validation, content filters) out of code and into runtime-evaluated expressions, enabling rule changes without redeployment. The engine is safe to expose to semi-trusted rule authors because it never touches objects, methods, or PHP internals. Key features include strict type evaluation (no coercion), safe mode for collecting all missing variables at once, variable aliasing, AST caching/export, custom function registration, and a clear error model with typed exceptions. Requires PHP 8.1+ and no additional extensions.

Key Features

Strict evaluation with no type coercion (e.g., 1 = '1' is a type error)
Safe mode that collects all missing variables instead of throwing on first error
Explain mode producing a tree showing evaluated leaves, pass/fail, short-circuits, and errors
Variable aliasing to translate between human-friendly names and technical paths
AST caching and export/import for pre-compilation and reuse
Custom function registration alongside built-ins
Clear error model with typed exceptions and structured details
Zero dependencies – runs on any standard PHP 8.1+ installation

Pros & Cons

Pros
  • Strict typing prevents silent bugs from type coercion
  • Explain mode makes it easy to debug and audit rule evaluations
  • Safe mode is useful for batch validation and reporting all errors
  • Zero dependencies simplifies integration and maintenance
  • Variable aliasing improves readability when non-developers author rules
  • AST caching and export improve performance for repeated evaluations
Cons
  • Cannot access object properties or call methods within expressions
  • Not suitable for complex expression needs like Symfony ExpressionLanguage or Twig
  • Requires PHP 8.1 or higher, excluding older PHP installations

Best For

Evaluate pricing rules without redeploying codeImplement eligibility checks for promotions or accessManage feature flags at runtimeValidate conditions in forms or workflowsApply content filters based on dynamic rules

FAQ

What is php-ruler?
php-ruler is a transparent expression rule evaluator written in pure PHP. It evaluates business rules stored as strings with strict typing and an explain mode that shows exactly why a rule passed or failed.
How does the explain mode work?
Explain mode turns any expression into a tree showing, leaf by leaf, what was evaluated, what passed or failed, what was short-circuited, and why something could not be evaluated (e.g., missing variable, type error).
What are the system requirements?
PHP 8.1 or higher. No PHP extensions, external services, or dependencies are required.
How do I install php-ruler?
Install via Composer: composer require ols/php-ruler
Can php-ruler access object properties or call methods?
No. php-ruler deliberately does not access objects, methods, or PHP internals to maintain strictness, safety, and dependency-free operation.